Java automatic boxing and automatic unpacking function
The first thing to note is the difference between the basic data type and the class, which is the difference between integers and int. Note that the container joins the object of the class rather than the base data type. The basic data types need to be packaged into objects first.
Integer i = 100; At compile time, the compiler will automatically perform the following syntax
Integer i = new integer (100); After the automatic boxing operation
public class AutoBoxDemo1
{
public static Voidmain (string[] args)
{
Integer i1= 127;
Integer i2= 127;
if (I1 ==i2)
System.out.println ("I1 = = I2");
Else
System.out.println ("I1!= i2");
}
}//This code will eventually display the correct, which is true, because for the number between-128-127, they are boxed in in memory after the integer object. and = = is used to denote the same object as comparison; If the value from –128 to 127 is exceeded, the boxed integer object is not reused, which is equivalent to creating a new integer object each time it is boxed. (Equals () compares the values (content) of two objects and = = = Compares two object references (memory address) is the same)
Integer i = 10; Packing
int t = i; Unboxing of course, in the operation, you can also automatically box and unboxing
System.out.println (i++);
The main differences for integers and int are:
Int I = 1; I do not have any method behind
Integer I = 1; I have a lot of ways behind it.
For a string type of case:
String str1 = "abc";
STRINGSTR2 = "ABC"; These two are references that point to the same object.
String Str3 =new string ("abc");
STRINGSTR4 = new String ("abc"); Generate two objects
The difference between the basic data type and its wrapper class is basically two-point difference:
Integer is the wrapper class of int, the initial value of int is 0, and the initial value of integer is null
Java intvalue () and valueof (), which converts an integer type to an int type, while valueof converts the given parameter to an integer type