Java autoboxing and unboxing

? ? What is automatic boxing and unboxing The automatic boxing (autoboxing) and unboxing (unboxing) of
basic data types are functions provided since J2SE 5.0.

Generally, when we want to create an object of a class, we will do this:

Class a = new Class(parameter);

When we create an Integer object, we can do this:

Integer i = 100; (Note: not int i = 100; )

In fact, when the above code is executed, the system executes it for us: Integer i = new Integer(100); This is the automatic boxing function of basic data types.

? ? The difference between basic data types and objects
Basic data types are not objects, that is, variables and constants defined using int, double, boolean, etc.

Primitive data types have no callable methods.

eg: int t = 1; t. is followed by no method drop.

Integer t = 1; t. There are many methods for you to call later.

? ? When is autoboxing
  For example : Integer i = 100;

    is equivalent to the compiler automatically compiling the following syntax for you: Integer i = new Integer(100);

? ? When is automatic unboxing
  Automatic unboxing (unboxing), that is, the basic data in the object is automatically taken out of the object. Automatic unboxing can be achieved as follows:

1 Integer i = 10; //boxing 
2 int t = i; //Unboxing


  When performing operations, automatic boxing and unboxing can also be performed.

1 Integer i = 10;   
2 System.out.println(i++);
? ? Integer's automatic boxing

//Numbers outside -128~127
Integer i1 = 200; 
Integer i2 = 200;         
System.out.println("i1==i2: "+(i1==i2));                  
// Numbers within -128~127
Integer i3 = 100; 
Integer i4 = 100; 
System.out.println("i3==i4: "+(i3==i4));



   The output result is:

    i1==i2 : false
    i3==i4: true


Description:

equals() compares whether the values ​​(contents) of two objects are the same.

"==" compares whether the references (memory addresses) of two objects are the same, and is also used to compare whether the values ​​of variables of two basic data types are equal.



In autoboxing, for values ​​between -128 and 127, after they are boxed as Integer objects, they will be stored in memory and reused,

so in the example, i3 and i4 actually refer to the same object.

If it exceeds the value from –128 to 127, the boxed Integer object will not be reused,

which is equivalent to creating a new Integer object every time it is boxed, so in the example, i1 and i2 refer to different objects.

In addition, when the autoboxing function is not used, the situation is the same as the normal class object, please see the following example:


1 Integer i3 = new Integer(100);
2 Integer i4 = new Integer(100);
3 System.out.println ("i3==i4: "+(i3==i4));//Display false
(thanks to Yi Zhiming's reminder O(∩_∩)O~)



? ? Unboxing of String Let's
look at an example:


1 String str1 = "abc";
2 String str2 = "abc";
3 System.out.println(str2==str1); //The output is true
4 System.out. println(str2.equals(str1)); //The output is true
5            
6 String str3 = new String("abc");
7 String str4 = new String("abc");
8 System.out.println(str3==str4); //The output is false
9 System.out.println(str3.equals(str4)); //



  How to explain the output of true? Can't seem to see anything. Then look at another example.

1 String d = "2";    
2 String e = "23";
3 e = e.substring(0, 1);
4 System.out.println(e.equals(d)); //The output is true     
5 System .out.println(e==d); //The output is false In 


the second example, the initial value of e is not the same as that of d, so e and d create an object respectively, (e==d) is false .
Similarly, str3 and str4 in the first example are also new objects, while str1 and str2 refer to the same object.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326655716&siteId=291194637