当前位置:网站首页>Basic data type and corresponding packing class

Basic data type and corresponding packing class

2022-06-23 12:23:00 Hard working boy


It seems that I haven't fully understood the difference between basic data types and packaging classes .

Basic data type

6 Number types :byte、short、int、long、float、double
1 Types of characters :char
1 Middle Boolean :boolean

The corresponding packaging class Byte、Short、Integer、Long、Float、Double、Character、Boolean

Number of digits of basic type 、 byte 、 The default value is

 Insert picture description here

Java Use in long The type of data must be followed by the number L, Otherwise, it will be parsed as an integer .
Java Use in long The type of data must be followed by the number L, Otherwise, it will be parsed as an integer .

Packing

Wrap basic types with their corresponding reference types .

Unpacking

Convert wrapper type to base data type .

For example, use Integer Three objects are created abc,a=b+c, because + Cannot be used in Integer Inter object operation , It will unpack automatically .

Wrapper classes and constant pools

Java Most of the basic types of wrapper classes implement constant pool technology , namely Byte, Short, Integer, Long,Character,Boolean.Byte,Short,Integer,Long The default value is created [-128,127] The corresponding type of cached data ,Character Created values in [0,127] Range cache data ,Boolean Go straight back to True Or False. If it goes beyond the corresponding range, it will still create new objects .

Two types of floating-point wrapper classes Float,Double Constant pool technology is not implemented .

Integer i1=40//Java  When compiling, it will directly encapsulate the code as  Integer i1=Integer.valueOf(40);, To use the objects in the constant pool .
Integer i2 = new Integer(40)// In this case, a new object is created .
System.out.println(i1==i2)// Output  false

原网站

版权声明
本文为[Hard working boy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231122351389.html