The basic difference between java type and type of packaging

Java type divided into two parts, one is the base type (primitive), such as int, double eight kinds of basic data types;
Another reference types (reference type), such as String, List and so on. And each primitive type and each correspond to a reference type, called type of packaging (or packing type, boxed primitive).
The main difference between the basic types and type of packaging is that the following three aspects:
1, only the value of the basic type, and the type of packaging having a value different from their identity (identity). This refers to the identity, if two objects point to the same reference, if the same object, then the identity. (. Similarly there equivalence)
view of a section of code:
 Integer = G new new Integer (0); 
 Integer h = new Integer(0);
System.out.println (g == h)
above code, although the two objects have the same value, but not the same point of the object, thus its reference output false.
Let's look at a piece of code:  
Integer a = 0;  
Integer b = 0;    
System.out.println (a == b);
This will output what it? Do not worry, look at a piece of code:  
Integer d = 128;  
Integer f = 128;  
System.out.println (f == d);
if you do not run these two codes, you know the answer, then I believe you have some knowledge of the constant pool.
In fact, when we assign a value directly to a int Integer time, it will call a valueOf () method. Therefore, as the code is equivalent to:
Integer Integer.valueOf = A (0);
Integer constant pool that is composed of -128 to 127. When we give an Integer value assigned on this range will be returned when the cache or the like from a the same reference, it will output a == b true. And more than this range, it will re-new object. Thus, f == d will output a false.
Now we look at a piece of code:  
int e = 128;  
Integer d = 128;  
System.out.println (e == d);
This will output what it? The answer is true.
Because when mixed in the basic type with a type of packaging operation, the package type is automatically unpacking. Thus, the comparison is actually int comparison values of e and d. 2, only the basic types of fully functional value, and the type of packaging in addition to all the functions of the corresponding base type, there is a non-functional value: null.
Turning now to a simple code:
 static Integer I; 
public static void main(String[] args) {  
  if(i == 128){   
  System.out.println("Unbelieveable~");  
  }
 }
What do you think will output it? do not know? Run your own look ~ ~ ~ ~
In fact, the code does not run correctly, it will be reported as a NullPointException abnormal, and why? Because i is defined when it is a reference to the Integer class, and i is not initialized, like all objects references, if not initialized then assigned a null value. Now that i is a null, then the already mentioned above, when used in combination with the basic type package type, package automatically unpacking. Now i do not point to any object, and therefore will be reported when unpacking a NullPointException exception. 3, the basic type is usually less time and space than the packaging type.
Look at the following code:  
·Long sum = 0L;    
·for(long i = 0;i<Integer.MAX_VALUE;i++)
·{
   sum +=i;
  }
  System.out.println (sum);
without a doubt, this code can be running, but the time spent will be longer. Because, when you declare a variable sum, accidentally declared as Long, but not long. Thus, in this cycle which will continue to boxing and unboxing, its performance will be significantly reduced. If Long changed long, so after my test, the time it takes the code will be only 1/5.
After these three comparisons, seemingly basic types have been feeling a victory type of packaging. However, in the following three places, the type of packaging would be more reasonable to use: 1, as a set of elements, key and value.
2, the parameterized type. For example: You can not write --ArryList <int>, you can only write ArrayList <Integer>.
3, when the call is reflection method.
In short, when you can choose the time, the basic type is a priority for the packaging type. Basic types easier and faster.
 

Guess you like

Origin www.cnblogs.com/xiaofuzi123456/p/11362630.html