A few strange but reasonable questions in Java from long and long

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.