java基本数据类型包括byte,short,int,long,float,double,boolean,char共8种,具体如下表。
数据类型 |
中文名 |
位数 |
有符号取值范围 |
无符号取值范围 |
byte |
字节 |
8 |
[-128,127]
|
[0,255] |
short |
短整型 |
16 |
[-2^15, 2^15-1]
|
[0,2^16-1] |
int |
整型 |
32
|
[-2^31, 2^31-1]
|
[0,2^32-1]
|
long |
长整型 |
64
|
[-2^63, 2^63-1]
|
[0,2^64-1] |
float |
浮点型 |
32
|
[1.4E-45, 3.4028235E38]
|
|
double |
双精度 |
64
|
[4.9E-324, 1.797E308]
|
|
boolean |
布尔型 |
|
{false,true} |
|
char |
字符型 |
|
[\u0000, \uffff] |
|
获取各种数据类型最大值和最小值:
/** * Created by CodeYang on 16/6/23. */ public class TypeExample { public static void main(String[] args) { System.out.println("Byte Min:" + Byte.MIN_VALUE); System.out.println("Byte Max:" + Byte.MAX_VALUE); System.out.println("Short Min:" + Short.MIN_VALUE); System.out.println("Short Max:" + Short.MAX_VALUE); System.out.println("Int Min:" + Integer.MIN_VALUE); System.out.println("Int Max:" + Integer.MAX_VALUE); System.out.println("Long Min:" + Long.MIN_VALUE); System.out.println("Long Max:" + Long.MAX_VALUE); System.out.println("Float Min:" + Float.MIN_VALUE); System.out.println("Float Max:" + Float.MAX_VALUE); System.out.println("Double Min:" + Double.MIN_VALUE); System.out.println("Double Max:" + Double.MAX_VALUE); System.out.println("Boolean True:" + true); System.out.println("Boolean False:" + false); System.out.println("Char Example1:" + 'A'); System.out.println("Char Example2:" + '9'); } }
输出结果:
Byte Min:-128 Byte Max:127 Short Min:-32768 Short Max:32767 Int Min:-2147483648 Int Max:2147483647 Long Min:-9223372036854775808 Long Max:9223372036854775807 Float Min:1.4E-45 Float Max:3.4028235E38 Double Min:4.9E-324 Double Max:1.7976931348623157E308 Boolean True:true Boolean False:false Char Example1:A Char Example2:9