Today, after the object is automatically converted to an integer type of judgment, encountered an incomprehensible point, when the value of more than 127, two of the same value of object object with = = To determine the result is false.
1Object A = 128;2Object B = 128;3 4 System.out.println (A.getclass (). GetName ());5 System.out.println (B.getclass (). GetName ());6System.out.println (a==b);7 8 9Object A1 = 127;TenObject B1 = 127; One A System.out.println (A1.getclass (). GetName ()); - System.out.println (B1.getclass (). GetName ()); -System.out.println (a1==B1); the - inta2 = 128; - intb2 = 128; - +System.out.println (A2==B2);
Results:
False
True
True
Before vaguely remember the value between 128 and 127, the Integer object will be special treatment, but the specific how to deal with the forgotten, the Internet to check some information finally understand the design behind the principle.
Data types in ①java are divided into basic data types and reference data types
int is the basic data type, and integer is the reference data type;
Ingeter is the wrapper class of int, and the initial value of int is null for the initial value of 0,ingeter;
② Automatic box packing and unpacking
Since the Java5.0 version has been added to the autoboxing feature, automatic unpacking and boxing is dependent on the JDK compiler during the compile-time preprocessing work.
A. Auto-boxing: Encapsulates the base data type as an object type, making it possible to invoke all the methods declared by an object later.
Integer InA = 127; // The above statement is the use of automatic boxing: resolved to New Integer (127);
B. Auto-Unpacking: Converts an object to a basic data type.
// Packing Integer InB =n; // Unpacking int InC = InB;
C. The typical use of automatic unpacking is when the operation is performed: Because the object cannot be operated directly, it needs to be converted to the basic data type before it can be subtraction.
Integer InD = 128;
System.out.println (ind--);
③ back to the question I encountered: Why is the value between 128 and 127 , two integer object equality can be judged by = =, but this range is not possible?
This is because Java's automatic boxing and unboxing design for integers and int is a pattern: the flyweight mode , in order to increase the reuse of simple numbers, Java defines: When automatically boxing for value from – Values between 128 and 127, which are boxed into an integer object, are reused in memory and always exist with only one object. If the value is exceeded, the boxed integer object is not reused, which is equivalent to creating an integer object each time it is boxed;
The above phenomenon is caused by the use of automatic boxing, if you do not use automatic boxing, but the same as the General class, with new to instantiate, each time new will be an object; This auto-boxing unboxing is not only used in the basic data types, but also in the string class.
④ Packing Class
All basic types have a corresponding class, that is, the wrapper class; it is immutable; the wrapper class is final and cannot define their subclasses.
Basic data types |
Packing class |
Byte |
Java.lang.Byte |
Boolean |
Java.lang.Boolean |
Short |
Java.lang.Short |
Char |
Java.lang.Character |
Int |
Java.lang.Integer |
Long |
Java.lang.Long |
Float |
Java.lang.Float |
Double |
Java.lang.Double |
Transferred from: http://www.cnblogs.com/hxliang/p/5454184.html
Java Integer and int deep understanding