[Java foundation] data types

The basic types and reference types

Java data types There are two types:

l basic types (also called built-in data types, or understood as a value type)  

l reference type  

 

The difference between the basic types and reference types

1.   Conceptually terms

Basic type: variable name pointing to specific values

Reference types: variable name points to the data stored in the memory address of an object

2.   from memory construct ways

Basic types: java After declaring variables will be immediately assigned to his memory

Reference Type: it in a particular manner ( similar to C pointer) points to an object entities (a specific value), no memory is allocated when such variable declarations, simply store a memory address.

3.   From the use of terms

Basic types: only assign particular value when used, using the "== it is determined " No

Reference types: use can be assigned null, use is determined equals Method

 

Java primitive types

Java -sized storage space occupied by each basic type is fixed. Their size, unlike most other random changes in hardware architecture as language changes. This invariance Java is one of the reasons the program relative to most other languages, the more easily transplanted.

All numeric types are signed, so do not go looking for unsigned numeric type.

The basic type of classification

Unlike most similar programming language, Java also supports numeric, string, Boolean data.

Java language provides . 8 basic types, are roughly classified into 4 categories

Integer

byte :. 8 bits and the maximum storage quantity of data is 255 , the data stored in the range of -128 to 127 between.

Short : 16 bit maximum data storage capacity is 65536 , the data range is -32768 to 32767 between.

int : 32 bits and the maximum data storage capacity of 2 32 th power minus 1 , minus the data range is 2 31 th to the n-2 31 th power minus one .

Long : 64 bits and the maximum data storage capacity of 2 64 th power minus 1 , data 2 is negative range 63 power 2 to the positive 63 th power minus one .

Float

a float : 32 bits data in the range of 3.4e-45 ~ 1.4e38 must be added after the number f, the direct assignment or F. .

Double : 64 bit, data in the range 324 ~ 1.8e308-4.9E , can be added to the assignment d or D may not be added.

Character

char : 16 bits to store Unicode code assignment in single quotes.

Boolean

boolean : Only true and false two values.

Basic Type Range table

Example: For the range of the basic type of numeric types, we do not need to remember mandatory, because their values have been defined in the corresponding class in the form of a package constant.

public class PrimitiveDemo {

     publicstaticvoid main(String[] args) {

         // byte

         The System. OUT .printf ( " basic types: byte  number of bits: .% D \ n-", Byte SIZE );

         . The System OUT .printf ( " packaging: % S \ n-", Byte. Class.getName ());

         System.out.printf("最小值(Byte.MIN_VALUE): 0x%x(十六进制), %d(十进制)\n", Byte.MIN_VALUE,  Byte.MIN_VALUE);

         System.out.printf("最大值(Byte.MAX_VALUE): 0x%x(十六进制), %d(十进制)\n\n", Byte.MAX_VALUE,  Byte.MAX_VALUE);

 

         // short

         System.out.printf("基本类型:short 二进制位数:%d\n", Short.SIZE);

         System.out.printf("包装类:%s\n", Short.class.getName());

         System.out.printf("最小值(Short.MIN_VALUE): 0x%x(十六进制), %d(十进制)\n", Short.MIN_VALUE,  Short.MIN_VALUE);

         System.out.printf("最大值(Short.MAX_VALUE): 0x%x(十六进制), %d(十进制)\n\n", Short.MAX_VALUE,  Short.MAX_VALUE);

 

         // int

         System.out.printf("基本类型:int 二进制位数:%d\n", Integer.SIZE);

         System.out.printf("包装类:%s\n", Integer.class.getName());

         System.out.printf("最小值(Integer.MIN_VALUE): 0x%x(十六进制), %d(十进制)\n", Integer.MIN_VALUE,  Integer.MIN_VALUE);

         System.out.printf("最大值(Integer.MAX_VALUE): 0x%x(十六进制), %d(十进制)\n\n", Integer.MAX_VALUE,  Integer.MAX_VALUE);

 

         // long

         System.out.printf("基本类型:long 二进制位数:%d\n", Long.SIZE);

         System.out.printf("包装类:%s\n", Long.class.getName());

         System.out.printf("最小值(Long.MIN_VALUE): 0x%x(十六进制), %d(十进制)\n", Long.MIN_VALUE,  Long.MIN_VALUE);

         System.out.printf("最大值(Long.MAX_VALUE): 0x%x(十六进制), %d(十进制)\n\n", Long.MAX_VALUE,  Long.MAX_VALUE);

 

         // float

         System.out.printf("基本类型:float 二进制位数:%d\n", Float.SIZE);

         System.out.printf("包装类:%s\n", Float.class.getName());

         System.out.printf("最小值(Float.MIN_VALUE): %s\n", Float.toString(Float.MIN_VALUE));

         System.out.printf("最大值(Float.MAX_VALUE): %s\n\n", Float.toString(Float.MAX_VALUE));

 

         // double

         System.out.printf("基本类型:double 二进制位数:%d\n", Double.SIZE);

         System.out.printf("包装类:%s\n", Double.class.getName());

         System.out.printf("最小值(Double.MIN_VALUE): %s\n", Double.toString(Double.MIN_VALUE));

         System.out.printf("最大值(Double.MAX_VALUE): %s\n\n", Double.toString(Double.MAX_VALUE));

 

         // char

         System.out.printf("基本类型:char 二进制位数:%d\n", Character.SIZE);

         System.out.printf("包装类:%s\n", Character.class.getName());

         System.out.printf("最小值(Character.MIN_VALUE): 0x%x(十六进制), %d(十进制)\n", (int)  Character.MIN_VALUE, (int) Character.MIN_VALUE);

         System.out.printf("最大值(Character.MAX_VALUE): 0x%x(十六进制), %d(十进制)\n\n", (int)  Character.MAX_VALUE, (int) Character.MAX_VALUE);

     }

}

输出:

基本类型:byte 二进制位数:8

包装类:java.lang.Byte

最小值(Byte.MIN_VALUE):  0x80(十六进制), -128(十进制)

最大值(Byte.MAX_VALUE):  0x7f(十六进制), 127(十进制)

 

基本类型:short 二进制位数:16

包装类:java.lang.Short

最小值(Short.MIN_VALUE):  0x8000(十六进制), -32768(十进制)

最大值(Short.MAX_VALUE):  0x7fff(十六进制), 32767(十进制)

 

基本类型:int 二进制位数:32

包装类:java.lang.Integer

最小值(Integer.MIN_VALUE):  0x80000000(十六进制), -2147483648(十进制)

最大值(Integer.MAX_VALUE):  0x7fffffff(十六进制), 2147483647(十进制)

 

基本类型:long 二进制位数:64

包装类:java.lang.Long

最小值(Long.MIN_VALUE):  0x8000000000000000(十六进制),  -9223372036854775808(十进制)

最大值(Long.MAX_VALUE):  0x7fffffffffffffff(十六进制),  9223372036854775807(十进制)

 

基本类型:float 二进制位数:32

包装类:java.lang.Float

最小值(Float.MIN_VALUE):  1.4E-45

最大值(Float.MAX_VALUE):  3.4028235E38

 

基本类型:double 二进制位数:64

包装类:java.lang.Double

最小值(Double.MIN_VALUE):  4.9E-324

最大值(Double.MAX_VALUE):  1.7976931348623157E308

 

基本类型:char 二进制位数:16

包装类:java.lang.Character

最小值(Character.MIN_VALUE):  0x0(十六进制), 0(十进制)

最大值(Character.MAX_VALUE):  0xffff(十六进制), 65535(十进制)

 

数据类型间的转换

自动转换

一般情况下,定义了某数据类型的变量,就不能再随意转换。但是JAVA允许用户对基本类型做有限度的类型转换。

如果符合以下条件,则JAVA将会自动做类型转换:

1)由数据转换为数据

显而易见的是,“小”数据类型的数值表示范围小于“大”数据类型的数值表示范围,即精度小于“大”数据类型。

所以,如果“大”数据向“小”数据转换,会丢失数据精度。比如:long转为int,则超出int表示范围的数据将会丢失,导致结果的不确定性。

反之,“小”数据向“大”数据转换,则不会存在数据丢失情况。由于这个原因,这种类型转换也称为扩大转换。 

这些类型由“小”到“大”分别为:(byteshortchar)< int < long < float < double。 

这里我们所说的“大”与“小”,并不是指占用字节的多少,而是指表示值的范围的大小。

 

2)转换前后的数据类型要兼容

由于 boolean 类型只能存放 true  false,这与整数或字符是不兼容的,因此不可以做类型转换。

 

3)整型类型和浮点型进行计算后,结果会转为浮点类型

long x = 30;
float y = 14.3f;
System.out.println("x/y = " + x/y);

运行结果

x/y = 1.9607843

可见 long 虽然精度大于 float 类型,但是结果为浮点数类型。

 

强制转换

在不符合自动转换条件时或者根据用户的需要,可以对数据类型做强制的转换。

转换方式为:在数值的前面用一个括号"()"把要强制转换的类型标注出来。

float f = 25.5f;
int x = (int)f;
System.out.println("x = " + x);

 

装箱和拆箱

Java中基本类型的包装类如下:

Byte <-> byte

Short <-> short

Integer <-> int

Long <-> long

Float <-> float

Double <-> double

Character <-> char

Boolean <-> boolean

 

装箱

装箱是将值类型转换为引用类型

例:

Integer i1 = new Integer(10); // 非自动装箱

Integer i2 = 10; // 自动装箱

System.out.println("i1 = " + i1);

System.out.println("i2 = " + i2);

 

拆箱

拆箱是将引用类型转换为值类型

int i1 = new Integer(10); // 自动拆箱

Integer tmp = new Integer(20);

int i2 = tmp.intValue(); // 非自动拆箱

System.out.println("i1 = " + i1);

System.out.println("i2 = " + i2);

 

为何需要装箱

一种最普通的场景是,调用一个含类型为Object的参数的方法,该Object可支持任意类型(因为Object是所有类的父类),以便通用。当你需要将一个值类型(如Int32)传入时,需要装箱。

另一种用法是,一个非泛型的容器,同样是为了保证通用,而将元素类型定义为Object。于是,要将值类型数据加入容器时,需要装箱。

 

自动装箱、自动拆箱

基本数据(Primitive)类型的自动装箱(boxing)、拆箱(unboxing)是自Java SE5开始提供的功能。

Java SE5之前的形式:

Integer i1 = new Integer(10); // 非自动装箱

Java SE5之后:

Integer i2 = 10; // 自动装箱

Guess you like

Origin www.cnblogs.com/deityjian/p/11408825.html