java-Basics-Automatic boxing with automatic unpacking leave our pits

Source: Internet
Author: User

In fact, Java in the process of automatic boxing and automatic unpacking, left a lot of pits to us, we will take the following as the basis of the integer class to discuss

In fact, the main concern here is two points

1. When using the integer x=1, this way to assign a value, in fact, the compiler when the 1 is a string, and then need to be converted through the ValueOf method, but in the process of conversion, in order to optimize the speed, he used the cache we do not know, because there will be some pits

The 2.Integer class overrides the Equals method, which not only compares the objects, but also compares the values inside.


The following code:(please carefully watch the note, which has provided the above two methods of the source code)

Package com.ray.object;/** * Automatic boxing with automatic unboxing pit * * @author Ray * @since 2015-05-04 * @version 1.0 * */public class Test {Publi c static void Main (string[] args) {//In fact, the following sentence after the assignment 1,1,1000,1000, are considered to be string,//and then through the integer.valueof () This method of conversion over,// Let's take a look at integer.valueof () This method of source/public static Integer valueOf (int i) {//if (I >= -128 && i <= integercache . High)//return integercache.cache[i + 128];//else//return new Integer (i);//}//in the source is actually cached part of the data, is -128-127//Therefore, in A==b is Return true//C==d is return falseinteger a = 1;integer b = 1;integer c = 1000;integer d = 1000; System.out.println ("a = = b----" + (A = = b));//TrueSystem.out.println ("c = = d----" + (c = = d));//false//This below is constructed of 4 inte The GER object comes out with an integer a_1 = new Integer (1), an integer b_1 = new Integer (1), an integer c_1 = new integer, and an integer d_1 = new Intege R (1000);//The following two sentences are through = = Contrast object, of course, is false System.out.println ("a_1 = = b_1----" + (a_1 = = b_1));//FalseSystem.out.println ("c_1 = = d_1----"+ (c_1 = = d_1));//false//The following two sentences are compared by equals, the integer class overrides the Equals method//See rewrite after the Equals method code//public boolean equals (Object obj) {///if (obj instanceof Integer) {//return value = = ((Integer) obj). Intvalue ();//}//return false;//}//We can see the comparison object, but also compare the value itself, so return to TrueSystem.out.println ("A_1.equals (b_1)----" + A_1.equals (b_1));//TrueSystem.out.println ("C_1.equals (d_1)----" + c_1.equals (d_1));//True}}



Output:

A = = B----True
c = = d----False
A_1 = = B_1----False
C_1 = = D_1----False
A_1.equals (b_1)----true
C_1.equals (d_1)----true




java-Basics-Automatic boxing with automatic unpacking leave our pits

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.