Add equals and "=" in java, and add equals in java

Source: Internet
Author: User

Add equals and "=" in java, and add equals in java

After JDK1.5, an Integer is automatically packed in the format of Integer I = 5;

The packing process is I = new Integer (4) in the memory. Everyone is familiar with this.

When this form is used, the equals usage remains unchanged, but "=" is slightly different.

See the following example:

(1)

Integer x = 12;
Integer y = 12;
System. out. println (x = y); // true
System. out. println (x. equals (y); // true

(2)

Integer x = 127;
Integer y = 127;
System. out. println (x = y); // true
System. out. println (x. equals (y); // true

Result and(1)Same

(3)

Integer x = 128;
Integer y = 128;
System. out. println (x = y); // false
System. out. println (x. equals (y); // true

(4)

Integer x = 129;
Integer y = 129;
System. out. println (x = y); // false
System. out. println (x. equals (y); // true

Result and(3)Same

Summary:The equals usage is the same as described in the previous blog.

But the usage of "=" has changed. This is because the data is automatically packed. If the data is packed in a byte (note that the data above is 127), the data will be shared, will not open up new space,

So the two new addresses are the same.

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.