primitive

Fastest most efficient way to determine decimal value is integer in Java

梦想与她 提交于 2019-12-05 03:56:21
Given a double variable named sizeValue and sizeValue contains something other than 0, what is the most efficient way to determine that sizeValue contains a value that is an integer? Currently i'm using sizeValue % 1 == 0 any other faster ways? give a try to Math.ceil : private static boolean isInt(double x) { return x == Math.ceil(x); } EDIT I've done some benchmarks with the following methods: private static boolean isInt1(double x) { return x == (int) x; } private static boolean isInt2(double x) { return x == Math.ceil(x); } private static boolean isInt3(double x) { return x % 1 == 0; }

Java — Primitive Counterpart of Byte, Integer, Long, etc. in template

时光怂恿深爱的人放手 提交于 2019-12-04 20:38:24
BACKGROUND : I am trying to implement a tiny template, i.e. generic class, which would allow me to achieve a pass-by-reference functionality as follows. public static class Ref<T> { T value; public Ref(T InitValue) { this.set(InitValue); } public void set(T Value) { this.value = Value; } public T get() { return this.value; } } So, I could define a function that takes a 'Ref' where the value can actually be changed, e.g. public static void function(Ref<Byte> x) { x.set((byte)0x7E); } The initialization of the variable to be passed by reference looks not so elegant. Ref<Byte> to_be_changed = new

Passing primitives to an OCMock's stub

余生长醉 提交于 2019-12-04 16:15:25
问题 I'm learning how to use OCMock to test my iPhone's project and I have this scenario: a HeightMap class with a getHeightAtX:andY: method, and a Render class using HeightMap . I'm trying to unit test Render using some HeightMap mocks. This works: id mock = [OCMockObject mockForClass:[Chunk class]]; int h = 0; [[[mock stub] andReturnValue:OCMOCK_VALUE(h)] getHeightAtX:0 andY:0]; Of course, works only for x=0 and y=0 . I want to test using a "flat" height map. This means I need to do something

Assigning result of an expression to a primitive

泄露秘密 提交于 2019-12-04 15:20:22
K.Sierra in her book "SCJP Study Guide" mentions "We know that a literal integer is always an int, but more importantly, the result of an expression involving anything int-sized or smaller is always an int." I've started experimenting and I'm a little bit confused with the below results: byte a = 1; // correct byte b = 1 + a; // incorrect (needs explicit casting) byte c = 1 + 1; // correct (I expected it to be incorrect) Could anyone explain to me why the last example does not require casting? Why does the Java compiler put the implicit cast? Is it because there are 2 int literals?

Using int as a type parameter for java.util.Dictionary

风格不统一 提交于 2019-12-04 09:53:09
问题 When I try to declare a Dictionary as such: private Dictionary<String, int> map; The compiler gives me the following error: Syntax error on token "int", Dimensions expected after this token But it works fine with Integer . I'm vaguely aware that Java treats int / Integer differently (I come from a .NET background), but I was hoping someone could give me a full explanation on why I can't use primitives in a Dictionary<> 回答1: In Java primitives aren't objects, so you can't use them in place of

How to create a Class of primitive array?

匆匆过客 提交于 2019-12-04 08:42:36
This question is derived from: How to get this Method object via reflection? I'm trying to do the following: Class c1 = Class.forName("[Ljava.lang.Integer;"); // works fine Class c1 = Class.forName("[Lint;"); // doesn't work, since it's primitive What is the workaround? int[].class is the only solution? Class c1 = Class.forName("[I"); See javadoc of Class.getName() for details. According to this page use: Class intArray = Class.forName("[I"); 来源: https://stackoverflow.com/questions/5076690/how-to-create-a-class-of-primitive-array

Can ModelAttribute be primitive?

江枫思渺然 提交于 2019-12-04 08:03:19
I am having a strange problem with ModelAttribute in Spring MVC 3.0. When I deploy the app at localhost, it works fine. But when I deploy the app on a remote server, it fails everytime user access a specific action, with the errors: ERROR: my.package.application.web.filter.ExceptionFilter - long.<init>() java.lang.NoSuchMethodException: long.<init>() at java.lang.Class.getConstructor0(Class.java:2706) at java.lang.Class.getDeclaredConstructor(Class.java:1985) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104) at org.springframework.web.bind.annotation.support

is there ever a use for primitive variables in javascript?

久未见 提交于 2019-12-04 04:40:33
问题 a pretty simple question, is there ever a case where using a primitive data-type is preferable in javascript, i am specifically bothered by primitive booleans, consider the following code var bool = new Boolean(false); if (bool){ alert(bool); } it will alert but you will get false , which is kinda confusing ( false != falsy). so is there ever a point in using primitive data-types and especially primitive booleans? 回答1: The primitive values are very useful (ex of primitive values: true, false,

Why is assigning 'int constant -> byte variable' valid, but 'long constant -> int variable' is not?

白昼怎懂夜的黑 提交于 2019-12-04 04:00:05
问题 I have this code snippet: int i = 5l; // not valid (compile error) byte b = 5; // valid What do you think about it? Why? 回答1: This is defined in the JLS #5.2 (Assignment conversion): If the expression is a constant expression (§15.28) of type byte, short, char, or int, a narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable. so: byte b = 5; //ok: b is a byte and 5 is

Move semantics and primitive types

本小妞迷上赌 提交于 2019-12-04 03:03:41
Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::cout << "Printing v1" << std::endl; print(v1); std::vector<int> v2(std::make_move_iterator(v1.begin()), std::make_move_iterator(v1.end())); std::cout << "Printing v1" << std::endl; print(v1); std::cout << "Printing v2" << std::endl; print(v2); std::vector<std::string> v3{"some", "stuff", "to", "put", "in", "the", "strings"}; std::cout << "Printing v3" << std::endl; print(v3); std::vector<std::string> v4(std::make_move_iterator(v3.begin()), std::make_move_iterator(v3.end())); std::cout << "Printing v3" << std: