当前位置:网站首页>Explain automatic packing and unpacking?
Explain automatic packing and unpacking?
2022-07-26 09:44:00 【Hua Weiyun】
Explain automatic packing and automatic unpacking ?
Automatic boxing : Convert basic data types back to objects
public class Test { public static void main(String[] args) { // Make a statement Integer object , Automatic packing is used : It can be interpreted as :Integer num = Integer.valueOf(9); Integer num = 9; } }
9 It belongs to the basic data type , In principle, it cannot be assigned to an object directly Integer Of . but jdk1.5 Started to introduce auto boxing / Unpacking mechanism , You can make such a statement , Automatically convert basic data types to corresponding encapsulation types , After becoming an object, you can call all the methods declared by the object .
Automatic dismantling : Convert objects back to basic data types
public class Test {
public static void main(String[] args) {
/ / Make a statement Integer object
Integer num = 9;
// The implication of calculation is automatic unpacking System.out.print(num--); } }
Because the object cannot be operated directly , It can only be added, subtracted, multiplied and divided after being converted to basic data type .
int and Integer What's the difference? ?
Integer yes int The wrapper class ;int It's the basic data type ;
Integer Variables must be instantiated before they can be used ;int Variables don't need ;
Integer It's actually a reference to an object , Point to this new Of Integer object ;int It stores data values directly ;
Integer The default value of is null;int The default value of is 0.
Two new Generated Integer Comparison of variables
because Integer Variable is actually for a Integer References to objects , So two passed new Generated Integer Variables are never equal ( because new Two objects are generated , Its memory address is different ).
Integer i = new Integer(10000);
Integer j = new Integer(10000);
System.out.print(i == j); //false
Integer Variables and int Comparison of variables
Integer Variables and int When comparing variables , As long as the values of two variables are equal , The result is true( Because of packaging Integer And basic data types int When comparing ,java Will automatically unpack as int, And then compare them , It's actually two int Comparison of variables )
int a = 10000;Integer b = new Integer(10000);Integer c=10000;System.out.println(a == b); // trueSystem.out.println(a == c); // true
Not new Generated Integer Variables and new Integer() Generate a comparison of variables
Not new Generated Integer Variables and new Integer() When the generated variables are compared , The result is false.( Because not new Generated Integer The variable points to java Objects in the constant pool , and new Integer() The generated variable points to the new object in the heap , They have different addresses in memory )
Integer b = new Integer(10000);Integer c=10000;
System.out.println(b == c); // false
Two non new Generated Integer Contrast of objects
For two non new Generated Integer object , When comparing , If the values of two variables are in the interval -128 To 127 Between , The comparison result is true, If the values of two variables are not in this range , The comparison result is false
Integer i = 100;
Integer j = 100;
System.out.print(i == j); //true
Integer i = 128;
Integer j = 128;
System.out.print(i == j); //false
On duty -128 ~ 127 Between time ,java Automatic packing will be carried out , The values are then cached , If you have the same value next time , It will be used directly in the cache . Caching is through Integer The inner class of IntegerCache To complete . When the value is out of this range , It's going to be in the pile new Create an object to store .
Give me a Integer The object is assigned a int When it's worth it , Would call Integer Class static methods valueOf, Source code is as follows :
public static Integer valueOf(String s, int radix) throws NumberFormatException {
return Integer.valueOf(parseInt(s,radix));
}
1
2
/**
(1) stay -128~127 within : In the static constant pool cache An array is static final type ,cache Array objects are stored in the static constant pool .
cache The elements in an array are not static final type , It is cache[k] = new Integer(j++),
So these elements are stored in the heap , It's just cache The array object stores the... In the heap Integer object ( Refer to the address )
(2) stay -128~127 outside : Create a new one Integer object , And back to .
*/
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high) {
return IntegerCache.cache[i + (-IntegerCache.low)];
}
return new Integer(i);
}
IntegerCache yes Integer The inner class of , Source code is as follows :/**
Cache supports automatic boxing of object identification semantics -128 and 127( contain ).
The cache is initialized on first use . The size of the cache can be determined by -XX:AutoBoxCacheMax = <size> Options control .
stay VM During initialization ,java.lang.Integer.IntegerCache.high Properties can be set and saved in private system properties
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty(“java.lang.Integer.IntegerCache.high”);
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
high = h;cache = new Integer[(high - low) + 1];int j = low;for(int k = 0; k < cache.length; k++) { cache[k] = new Integer(j++); // Create an object }
}
private IntegerCache() {}
}
What is reflection ?
Reflection is in the running state , For any class , Can know all the properties and methods of this class ; For any object , Can call any of its methods and properties ; The function of dynamically obtaining information and dynamically calling methods of objects is called Java The reflexive mechanism of language .
What are the advantages and disadvantages of reflection mechanism ?
advantage : Be able to get instances of classes dynamically at runtime , Increase flexibility ; It can be combined with dynamic compilation Class.forName(‘com.mysql.jdbc.Driver.class’);, load MySQL Driver class .
shortcoming : The use of reflection performance is low , Need to parse bytecode , Parse objects in memory . The solution is : adopt setAccessible(true) close JDK Safety check to improve reflection speed ; When you create an instance of a class multiple times , It's much faster with caching ;ReflflectASM Tool class , Speed up reflection by bytecode generation .
边栏推荐
猜你喜欢
配置ADCS后访问certsrv的问题
SSG框架Gatsby访问数据库,并显示到页面上
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
uni-app学习总结
解决npm -v突然失效 无反应
R language ggplot2 visualization: align the legend title to the middle of the legend box in ggplot2 (default left alignment, align legend title to middle of legend)
Search module use case writing
Fiddler download and installation
Neural network and deep learning-6-support vector machine 1-pytorch
[Online deadlock analysis] by index_ Deadlock event caused by merge
随机推荐
官方颁发的SSL证书与自签名证书结合实现网站双向认证
反射机制的原理是什么?
QT handy notes (II) edit control and float, qstring conversion
m进制数str转n进制数
Calling DLL to start thread
Malloc failed to allocate space and did not return null
微信小程序图片无法显示时显示默认图片
正则表达式
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
js 表格自动循环滚动,鼠标移入暂停
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
2022 zhongkepan cloud - server internal information acquisition and analysis flag
面试题目大赏
QT handy notes (VI) -- update interface, screenshot, file dialog box
青少年软件编程等级考试标准解读_二级
服务器环境配置全过程
解释一下自动装箱和自动拆箱?
Development to testing: a six-year road to automation starting from 0
2020-12-29