An issue in Java where integer values are less noticeable

Source: Internet
Author: User

Today, to help people look at the code, find this problem, review it, record it.

Let's take a look at a code snippet

public static void Main (string[] args) {Integer a1 = integer.valueof;  Danielinbiti    Integer B1 =;      System.out.println ("1:=" + (a1 = = B1));       Integer A2 =;      Integer b2 =;      System.out.println ("2:=" + (a2 = = b2));      Integer a3 = new Integer;      Integer B3 =;      System.out.println ("3:=" + (a3 = = B3));      Integer a4 = 129;      Integer b4 = 129;      System.out.println ("4:=" + (a4 = = b4));  }

The result of this code comparison, if not executed do not know what the answer is in your heart.

To know the answer, it involves the Java buffer and heap problems.

The number of integer types in Java is buffer-taken for numbers between 128-127, so the equals sign is consistent. But the number that is not in this interval is new in the heap. Therefore, the address space is not the same, it is not equal.

Integer b3=60, which is a boxing process that is also an integer b3=integer.valueof (60)

Therefore, it is necessary to use Intvalue () to see if the integer comparison value is equal.

There is no buffer for double.

Answer

1:=true
2:=true
3:=false
4:=false

An issue in Java where integer values are less noticeable

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.