当前位置:网站首页>Difference between package type and basic type

Difference between package type and basic type

2022-06-24 10:23:00 Ugly and ugly

Java There is 8 Basic types of data in , Respectively :

6 Number types :byte、short、int、long, These four types are integer numbers ;float、double, These two types are floating point numbers ;

1 Kind of boolean type :boolean, namely true or false;

1 Types of characters :char.

this 8 The corresponding packaging types of the three basic types are :Byte、Short、Integer、Long、Float、Double、Boolean、Character.

Basic types Packaging type byte digit The default value is Range
byteByte180-128~127
shortShort2160-32768~32767
intInteger4320-2147483648~2147483647
longLong8640L-9223372036854775808 ~ 9223372036854775807
floatFloat4320F1.4E-45 ~ 3.4028235E38
doubleDouble8640D4.9E-324 ~ 1.7976931348623157E308
booleanBoolean18falsefalse、true
charCharacter1'u0000'0~65535

about boolean type , Official documents do not clearly define , It depends on JVM The specific implementation of the manufacturer . Logically, it means to occupy 1 position , But in practice, we will consider the factor of efficient computer storage . in addition ,Java The storage space occupied by each basic type of will not change with the change of machine hardware architecture like most other languages . The invariance of the amount of storage space occupied is Java One of the reasons why programs are more portable than programs written in most other languages .

in addition ,Java Use in long、float、double Type of data, it is best to add an identification after the value L、F、D perhaps l( A lowercase letter L)、f、d, Otherwise, the value will be parsed as integer data . also char Type is marked with single quotation marks , What is marked in double quotation marks is a string .

The difference between basic types and packaging types

(1) For member variables , If packing type is used , If the value is not assigned, the default value is null, There are default values for basic types

(2) The packaging type can be used for generic , And the basic type can't be .

(3) Local variables of basic data types are stored in Java In the local variables table in the virtual machine stack , Member variables of the basic data type ( Not by  static  modification ) Store in Java In the heap of the virtual machine . The wrapper type belongs to the object type , Almost all object instances exist in the heap .

(4) Compared to object types , Basic data types take up very little space .

It is a common misconception that basic data types are stored in the stack . If the member variable of the basic data type is not  static  Embellished words , It's in the pile . Generally speaking, it is not recommended to use static Modify member variables of basic types , The packing type corresponding to the basic data type should be used .

This article references from :Java Basic and common knowledge & Interview question summary ( On ) | JavaGuide

原网站

版权声明
本文为[Ugly and ugly]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240916245505.html