Questions:For example, sometimes the JSON data network transmission, the client we use long to express, but the server we use a long, sometimes we want to Long is null (for example, some self-increment ID can not set the ID value), but there is always a value, make a half day inexplicable.
Solution:Changes the client's long to a long.
Reason:It turned out that our client did not set the ID, but because a long was used, thisThe default value is 0 ., the service side of the resolution when it became 0.
Analysis:The compiler requires initialization if the long type is a local variable. In other cases, the default value is 0.
--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------
question: look directly at the following code:
Long A1 = 1l; Long B1 = 1l; SYSTEM.OUT.PRINTLN (a1 = = B1); Long A2 = new long (1); Long b2 = 1l; SYSTEM.OUT.PRINTLN (A2 = = b2);
Sometimes do not pay attention to encounter this problem, two long with = = to compare, and then their own inexplicable a bit.
Solution:For long, replace = = with equals.
Reason:= = Comparison ofmemory Address。
Analysis:As the code above, the 1l memory address exists in the JVM'sConstant Pool, A1 and B1 all point to this address, so A1==B1 is true. A2 is a new one obtained through theHeap MemoryThe address in, certainly, and the 1l memory address in the constant pool are not equal, so a2==b2 is false.
outside the question:Equals is the method of object, and by default it compares memory addresses, but if overridden, it looks like the overwrite of long:
public boolean equals (Object obj) { if (obj instanceof Long) { return value = = ((Long) obj). Longvalue (); return false;}
You can see that this is the value constant of the comparison long, so ...
Code:Https://github.com/ACC-GIT/ACCTestJava/blob/master/src/com/acc/test/java/LongTest.java
A few strange but reasonable questions in Java from long and long