In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the introduction of Java basic data types and packaging classes". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "the introduction of Java basic data types and packaging classes"!
Keyword
Digits, constant pool, packing class (automatic packing and unpacking)
8 basic data types
Integer type: byte,short,int,long
Floating point type: float,double
Character type: char
Boolean type: boolean
Number of bits
1. In the JVM specification, boolean is treated as int, accounting for 4 bytes, and the boolean array is treated as an byte array, accounting for 1 byte.
2. The byte type is 1byte, which is 8 digits, and the number that can be represented is-128to127because there is another zero, which adds up to 256.
Packaging class
It can be solved.
1. The basic type does not have the characteristics of an object: there are no member variables, and methods can be called.
2. Do not support the object-oriented transformation mechanism (immutable), packaging classes. Byte,Short,Integer,Long,Double,Float,Charater,Boolean .
JDK1.5, began to provide automatic boxing and unpacking functions, basic type variables assigned to the corresponding wrapper class variables are called boxing. Automatic unpacking is the opposite.
Java 7 enhances the wrapper class by providing a static compare (xxx val1,xxx val2) method for all wrapper classes to compare the size of the two basic types. (after solving the problem of two 128 automatic boxing, comparing their sizes is not equal.)
Java 8 again enhances the wrapper class to support unsigned operations.
The difference between basic types and wrapper classes
1. The declaration method is different: the wrapper type needs to use the new keyword to allocate storage space in the heap, while the basic type does not.
2. The storage mode and location are different: the basic type is stored in the stack, while the wrapper type is in the heap, and then used by reference.
3. The initial value is different: the initial value of the wrapper type is null, and the initial value of the basic type is eg:int-> 0PowerBoolean-> false.
4. Different scenarios: wrapper classes (immutable) must be used for commonly used collections such as Collection and Map.
Constant pool
Benefits:
1. Save memory space
2. Improve operation efficiency
Packaging class
There are 5 kinds of Byte,Short,Integer,Long,Character,Boolean. Constant pool is used in the range of-128to 127. it will be unpacked and boxed automatically, and new instances will be created for the rest of the number range.
The wrapper class Float,Double of two floating point types does not implement constant pool technology.
Basic type
When decorated by final:
If it is a member variable, it must be stored in the constant pool, such as variable b
If it is a local variable, it is neither stored in the constant pool nor in the stack
When not modified by final:
Both member variables and local variables are not necessarily stored in the constant pool. Variables of int type will be based on the power of 2 to the 15th power. If the number is greater than or equal to this number, it will be stored in the constant pool. If it is less than, it will not.
But the double type 1D will not, 2d will, and this reason has not been found.
Frequently asked questions about packaging
one。 Numerical packaging class
/ Integer case /
Integer i1 = 128
Integer i2 = 128
Integer i3 = 127
Integer i4 = 127
System.out.println (i1 = = i2); / / false
System.out.println (i3==i4); / / true
/ Long case /
Long L1 = 128L
Long L2 = 128L
Long L3 =-128L
Long L4 =-128L
System.out.println (L1 = = L2); / / false
System.out.println (l3==l4); / / true
/ Float (Double) case (contrast) /
Float F1 = 128f
Float f2 = 128f
Float f3 = 127f
Float f4 = 127f
System.out.println (F1 = = f2); / / false
System.out.println (f3==f4); / / false
two。 Principle
Because Byte,Integer,Long caches data between-128 and 127, if the value I to be converted is in that range, it is directly cached from the cache instead of new Integer (I).
Float and Double are new a new one every time, and there is no caching mechanism, so both are false.
3. JDK source code:
/ implementation of Integer /
Public static Integer valueOf (int I) {
If (I > =-128 & & I =-128 & & l
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.