Java 类型

56 阅读4分钟

基础类型

基础类型使用频繁,占用空间小,存入栈空间,可以实现快速读写操作。 对应基础类型的封装类型需要创建实例,是存入的堆内存,既消耗资源,有耗费时间。 堆内存,用于存放Java对象实例,如new ArrayList()new关键字来创建的的对象。堆内存依靠Java来自动进行垃圾回收,释放内存资源。 栈内存,读写速度仅次于寄存器,用于存储小量快速读写的数据。如int等基本类型。同时栈内存的数据是可以共享的。int a =3;int b =3;在执行该语句时,首先会在栈内存创建a =3,之后在创建b=3时会去栈内存中检索是有已经存在3这个值,存在即建立b3的地址指向。

整数类型
  • byte
  • short
  • int
  • long
浮点类型(小数类型)
  • float
  • double
布尔类型(ture/false)
  • boolean
单字符类型
  • char
引用类型
  • String

byte

定义
  • byte 数据类型是8位、有符号的,以二进制补码表示的整数
  • 取值范围:(-2^7,2^7)
  • 默认值:0
  • byte 类型用在大型数组中节约空间,主要代替整数,因为 byte 变量占用的空间只有 int 类型的四分之一
代码
public static void main(String[] args) {  
	byte b = 0;  
	System.out.println("byte默认值:"+b);  
	// Java中对于平方的运算采用Math库来进行,这个地方用到了类型强制转换  
	b = (byte) Math.pow(-2,7);  
	System.out.println("byte最小值:"+b);  
	b = (byte) (Math.pow(2,7)-1);  
	System.out.println("byte最大值:"+b);  
	System.out.printf("byte范围:(%d,%d)\n",Byte.MIN_VALUE,Byte.MAX_VALUE);
}

// 输出
byte默认值:0
byte最小值:-128
byte最大值:127
byte范围:(-128,127)

short

定义
  • short 数据类型是 16 位、有符号的以二进制补码表示的整数
  • 取值范围:(-2^15,2^15-1)
  • 默认值:0
  • Short 数据类型也可以像 byte 那样节省空间。一个short变量是int型变量所占空间的二分之一
代码
public static void main(String[] args) {  
    short s = 0;  
    System.out.println("short默认值:"+s);  
    s = (short) Math.pow(-2,15);  
    System.out.println("short最小值:"+s);  
    s = (short) (Math.pow(2,15)-1);  
    System.out.println("short最大值:"+s);  
    System.out.printf("short范围:(%d,%d)\n",Short.MIN_VALUE,Short.MAX_VALUE);  
}

// 输出
short默认值:0
short最小值:-32768
short最大值:32767
short范围:(-32768,32767)

int

定义
  • int 数据类型是32位、有符号的以二进制补码表示的整数
  • 取值范围:(-2^31,2^31-1)
  • 默认值:0
  • 一般地整型变量默认为 int 类型
代码
public static void main(String[] args) {  
    int s = 0;  
    System.out.println("int默认值:"+s);  
    s = (int) Math.pow(-2,31);  
    System.out.println("int最小值:"+s);  
    s = (int) (Math.pow(2,31)-1);  
    System.out.println("int最大值:"+s);  
    System.out.printf("int范围:(%d,%d)\n",Integer.MIN_VALUE,Integer.MAX_VALUE);  
}

//输出
int默认值:0
int最小值:-2147483648
int最大值:2147483647
int范围:(-2147483648,2147483647)

long

定义
  • long 数据类型是 64 位、有符号的以二进制补码表示的整数
  • 取值范围:(-2^63,2^63-1)
  • 默认值:0
  • 这种类型主要使用在需要比较大整数的系统上
代码
public static void main(String[] args) {  
    long s = 0;  
    System.out.println("long默认值:"+s);  
    s = (long) Math.pow(-2,31);  
    System.out.println("long最小值:"+s);  
    s = (long) (Math.pow(2,31)-1);  
    System.out.println("long最大值:"+s);  
    System.out.printf("long范围:(%d,%d)\n",Long.MIN_VALUE,Long.MAX_VALUE);  
}

// 输出
long默认值:0
long最小值:-2147483648
long最大值:2147483647
long范围:(-9223372036854775808,9223372036854775807)

float

定义
  • float 数据类型是单精度、32位、符合IEEE 754标准的浮点数
  • 取值范围:(-2^128,2128)、(-3.40E+38,3.40E+38)
  • 默认值:0.0f
代码
public static void main(String[] args) {  
    float f = 0;  
    System.out.println("float默认值:"+f);  
    f = 0.12f;  
    System.out.println("float赋值:"+f);  
    f = 0.123456789123456789123456789f;  
    System.out.println("float赋值:"+f);  
    f = -0.123456789123456789123456789f;  
    System.out.println("float赋值:"+f);  
    f = -3.40e+38f;  
    System.out.printf("float最小值:%f\n",f);  
    f = 3.40e+38f;  
    System.out.printf("float最大值:%f\n",f);  
    System.out.printf("float在Java封装类Float中的取值范围:(%f,%f)\n",Float.MIN_VALUE,Float.MAX_VALUE);  
}

// 输出
float默认值:0.0
float赋值:0.12
float赋值:0.12345679
float赋值:-0.12345679
float最小值:-339999995214436420000000000000000000000.000000
float最大值:339999995214436420000000000000000000000.000000
float在Java封装类Float中的取值范围:(0.000000,340282346638528860000000000000000000000.000000)

double

定义
  • double 数据类型是双精度、64 位、符合 IEEE 754 标准的浮点数
  • 取值范围:(-2^1024,2^1024)、(-1.79E+308,1.79+308)
  • 默认值:0.0d
代码
public static void main(String[] args) {  
    double d = 0;  
    System.out.println("double默认值:"+d);  
    d = 0.12d;  
    System.out.println("double赋值:"+d);  
    d = 0.123456789123456789123456789d;  
    System.out.println("double赋值:"+d);  
    d = -0.123456789123456789123456789d;  
    System.out.println("double赋值:"+d);  
    d = -1.79e+308d;  
    System.out.printf("double最小值:%f\n",d);  
    d = 1.79e+308d;  
    System.out.printf("double最大值:%f\n",d);  
    System.out.printf("double在Java封装类型取值范围:(%f,%f)\n",Double.MIN_VALUE,Double.MAX_VALUE);  
}

//输出
double默认值:0.0
double赋值:0.12
double赋值:0.12345678912345678
double赋值:-0.12345678912345678
double最小值:-179000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
double最大值:179000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
double在Java封装类型取值范围:(0.000000,179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000)

boolean

定义
  • boolean数据类型表示一位的信息
  • 取值范围:true、false

char

定义
  • char 类型是一个单一的 16 位 Unicode 字符
  • 取值范围:(\u0000,\uffff)
  • char 数据类型可以储存任何字符