当前位置:网站首页>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 .
边栏推荐
- POJ 1012 Joseph
- Force deduction brush questions, sum of three numbers
- EOJ 2020 1月月赛 E数的变换
- 音视频知识
- QT handy notes (VI) -- update interface, screenshot, file dialog box
- After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"
- JS 连等赋值操作
- spolicy请求案例
- Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
- CSV data file settings of JMeter configuration components
猜你喜欢
配置ADCS后访问certsrv的问题
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)
2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
The problem of accessing certsrv after configuring ADCs
MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
The diagram of user login verification process is well written!
图解用户登录验证流程,写得太好了!
B站这个视频我是跪着看完的
【荧光字效果】
服务器、客户端双认证(2)
随机推荐
MySQL的逻辑架构
2019 ICPC Asia Yinchuan Regional(水题题解)
(一)面扫描仪与机械臂的手眼标定(眼在手上)
Gauss elimination for solving XOR linear equations
学习笔记之常用数组api 改变原数组和不改变原数组的有哪些?
AR model in MATLAB for short-term traffic flow prediction
JS table auto cycle scrolling, mouse move in pause
IIS website configuration
莫队学习总结(二)
QT随手笔记(六)——更新界面、截图、文件对话框
Logical architecture of MySQL
After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"
SSG framework Gatsby accesses the database and displays it on the page
Gauss elimination solves the inverse of matrix (Gauss)
Network flow learning notes
spolicy请求案例
网站设计需要的基本知识
mfc随手笔记
2022 zhongkepan cloud - server internal information acquisition and analysis flag
CSV data file settings of JMeter configuration components