Java数据类型
Java数据类型概述
在Java中数据类型主要分为两类:基本数据类型和引用数据类型。
基本数据类型
基本数据类型主要分成四大类,八小种
四类:整型、浮点型、字符型以及布尔型
八小种
数据类型 | 关键字 | 内存占用 | 范围 |
---|---|---|---|
字节型 | byte | 1 字节 | -128 ~ 127 |
短整型 | short | 2 字节 | -32768 ~ 32767 |
整型 | int | 4 字节 | ~ |
长整型 | long | 8 字节 | ~ |
单精度浮点数 | float | 4 字节 | 有范围,一般不关注 |
双精度浮点数 | double | 8 字节 | 有范围 |
字符型 | char | 2 字节 | 0 ~ 65535 |
布尔型 | boolean | 没有明确规定 | true 和 false |
以下为部分代码
public static void main(String[] args) {
//int 4个字节 同意的不管你多少位
int a =1;
System.out.println(a);
System.out.println("hello");
System.out.println("===========");
//长整型 8个字节
long b= 10L;
System.out.println(b);
System.out.println(Long.MAX_VALUE);
System.out.println(Long.MIN_VALUE);
System.out.println("===========");
//短整型 两个字节
short sh=10;
System.out.println(sh);
System.out.println(Short.MAX_VALUE);
System.out.println(Short.MIN_VALUE);
System.out.println("===========");
//字节型 一个字节
byte f=11;
System.out.println(f);
System.out.println(Byte.MAX_VALUE);
System.out.println(Byte.MIN_VALUE);
System.out.println("===========");
//双精度浮点数 八个字节
double c=10;
System.out.println(c);
System.out.println(Double.MAX_VALUE);
System.out.println(Double.MIN_VALUE);
System.out.println("===========");
//单精度浮点数 四个字节
float d =10;
System.out.println(d);
System.out.println(Float.MIN_VALUE);
System.out.println(Float.MAX_VALUE);
System.out.println("===========");
//字符类型 两个字节
char ch = 'A';//没有负数的
System.out.println(ch);
char ch2 = '高';
System.out.println(ch2);
System.out.println("===========");
//布尔类型
boolean flg=true;
//0是假 1是真 但是在Java中真假只有true和false
System.out.println(flg);
//不能和任何类型进行转换 boolean的包装类是Boolean
}
}
以下为输出结果
1
hello
===========
10
9223372036854775807
-9223372036854775808
===========
10
32767
-32768
===========
11
127
-128
===========
10.0
1.7976931348623157E308
4.9E-324
===========
10.0
1.4E-45
3.4028235E38
===========
A
高
===========
true