Home >Java >javaTutorial >Talk about automatic encapsulation, unboxing and example analysis in Java data types

Talk about automatic encapsulation, unboxing and example analysis in Java data types

零下一度
零下一度Original
2017-06-23 10:04:211970browse

Purpose:

Autoboxing and unboxing were introduced starting with Java 1.5 for the purpose of converting primitive type values Automatically convert to the corresponding object , to use the object's API and reference type operations. The automatic boxing and unboxing mechanism makes it simpler and more direct for us to use primitive types or object types in Java's variable assignment or method invocation.

Definition:

Autoboxing isJavaautomatically converts primitive type values ​​into Corresponding objects, such as converting int variables into Integer objects, this process is called boxing, and vice versa Integer objects are converted into int type values. This process is called unboxing. Because the boxing and unboxing here are automatic and non-human conversions, they are called automatic boxing and unboxing. The corresponding encapsulation classes for primitive types byte, short, char, int, long, float, double and boolean are Byte,Short,Character,Integer,Long,Float,Double,Boolean.

Implementation:

When automatically boxing, the compiler calls valueOf to convert the original type value into an object, and automatically When unboxing, the compiler converts the object into a primitive type value by calling methods like intValue(), doubleValue().

Occurrence time:

There is a method that accepts a parameter of object type. If we pass a primitive type value, then Java will automatically convert this primitive type value into the corresponding object.

List<Integer> list = new ArrayList<Integer>();
// 自动装箱
list.add(1);
list.add(2);
// 拆箱
int i = list.get(0);
int ii = list.get(1);

Disadvantages of automatic boxing:

There is a problem with automatic boxing, that is, the automatic boxing operation is performed in a loop, as follows The above example will create redundant objects and affect the performance of the program.

Integer sum = 0;
 for(int i=1000; i<5000; i++){
   sum+=i;
}

The above code sum+=i can be regarded as sum = sum + i, but ==,+,-, *, /This operator does not apply to IntegerObject, first sum performs the automatic unboxing operation, performs the numerical addition operation, and finally the automatic boxing operation conversion occurs into an Integer object. The process is as follows

int temp = sum.intValue() + i;
Integer sum = new Integer(temp);

Since the sum we declare here is of Integer type, it will be in the above loop Creating nearly 5000 useless Integer objects in such a huge cycle will reduce the performance of the program and increase the workload of garbage collection. Therefore, you need to pay attention to this and declare variable types correctly to avoid performance problems caused by automatic boxing.

Notes:

Autoboxing and unboxing can make the code concise, but we should pay attention when using it, otherwise some problems will occur.

1.比较

”==“可以用于原始值进行比较,也可以用于对象进行比较,当用于对象与对象之间比较时,比较的不是对象代表的值,而是检查两个对象是否是同一对象,即检查引用地址是否相同。这个比较过程中没有自动装箱发生。进行对象值比较不应该使用”==“,而应该使用对象对应的equals方法

// 1
int i1=1;
int i2=1;
System.out.println(i2==i1);// true
// 2
Integer I1=1;
System.out.println(I1==i1);// true
Integer I2=1;
System.out.println(I1==I2);// true
// 3
Integer I3 = 128;// 触发自动封装
Integer I4 = 128;
System.out.println(I3==I4);// false
// 4
Integer I5= new Integer(1);// 不触发自动封装
Integer I6= new Integer(1);
System.out.println(I5==I6); // false

值得注意的是第2个小例子的第二个比较,这是一种极端情况。I1和I2的初始化都发生了自动装箱操作。但是处于节省内存的考虑,JVM会缓存-128到127的Integer对象。因为I1和I2实际上是同一个对象。所以使用”==“比较返回true,而第三个小例子使用‘==’返回false

注:自动封装的函数

public static Integer valueOf(int i) {
 return i >= 128 || i < -128 ? new Integer(i) : SMALL_VALUES[i + 128];
 }
private static final Integer[] SMALL_VALUES = new Integer[256];

2.容易混乱的对象和原始数据值

另一个需要避免的问题就是混乱使用对象和原始数据值,一个具体的例子就是当我们在一个原始数据值与一个对象进行比较或赋值时,如果这个对象没有进行初始化或者为Null,在自动拆箱过程中obj.xxxValue,会抛出NullPointerException,如下面的代码

private static Integer count;
if(count>=0){
System.out.println(11111111111L);
}

3.缓存的对象

Java中,会对-128到127的Integer对象进行缓存,当创建新的Integer对象时,如果符合这个这个范围,并且已有存在的相同值的对象,则返回这个对象,否则创建新的Integer对象。

4.生成无用对象

因为自动装箱会隐式地创建对象,像前面提到的那样,如果在一个循环体中,会创建无用的中间对象,这样会增加GC压力,拉低程序的性能。所以在写循环时一定要注意代码,避免引入不必要的自动装箱操作。

The above is the detailed content of Talk about automatic encapsulation, unboxing and example analysis in Java data types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn