2.3 数据类型

Java数据类型默认值
| 数据类型 | 默认值 |
|---|---|
字节型(byte) | 0 |
短整型(short) | 0 |
整型(int) | 0 |
长整型(long) | 0 或 0L |
单精度(float) | 0.0 或 0.0f |
双精度(double) | 0.0 或 0.0d |
字符型(char) | ‘u0000’ |
布尔型(boolean) | false |
字符串型(String) | null |
| 引用数据类型 | null |
测试代码:
package datatype;
public class DataDefault {
static byte by;
static short sh;
static int i;
static long l;
static float f;
static double d;
static char ch;
static boolean bool;
static String str;
public static void main(String[] args) {
System.out.println("byte :"+by);
System.out.println("short :"+sh);
System.out.println("int :"+i);
System.out.println("long :"+l);
System.out.println("float :"+f);
System.out.println("double :"+d);
System.out.println("char :"+ch);
System.out.println("boolean:"+bool);
System.out.println("String :"+str);
}
}
测试结果:
byte :0
short :0
int :0
long :0
float :0.0
double :0.0
char :
boolean:false
String :null
数据类型的数据范围及占用内存空间
| 数据类型 | 占用位(比特)bit | 占用字节byte | 数据范围 |
|---|---|---|---|
| byte | 8 | 1 | -128~127 |
| short | 16 | 2 | -32768~32767 |
| int | 32 | 4 | -2147483648~2147483647 |
| long | 64 | 8 | -9223372036854775808~9223372 036854775807 |
| float | 32 | 4 | 1.4x10-45~3.4028235x1038 |
| double | 64 | 8 | 4.9x10-324~1.7976931348623157x10308 |
| char | 16 | 2 | 0~65535 |
| boolean | 1 | true 或 false |
测试代码:
package datatype;
public class DataType {
public static void main(String[] args) {
// byte
System.out.println("基本类型:byte 二进制位数:" + Byte.SIZE);
System.out.println("最小值:" + Byte.MIN_VALUE);
System.out.println("最大值:" + Byte.MAX_VALUE);
System.out.println();
// short
System.out.println("基本类型:short 二进制位数:" + Short.SIZE);
System.out.println("最小值:" + Short.MIN_VALUE);
System.out.println("最大值:" + Short.MAX_VALUE);
System.out.println();
// int
System.out.println("基本类型:int 二进制位数:" + Integer.SIZE);
System.out.println("最小值:" + Integer.MIN_VALUE);
System.out.println("最大值:" + Integer.MAX_VALUE);
System.out.println();
// long
System.out.println("基本类型:long 二进制位数:" + Long.SIZE);
System.out.println("最小值:" + Long.MIN_VALUE);
System.out.println("最大值:" + Long.MAX_VALUE);
System.out.println();
// float
System.out.println("基本类型:float 二进制位数:" + Float.SIZE);
System.out.println("最小值:" + Float.MIN_VALUE);
System.out.println("最大值:" + Float.MAX_VALUE);
System.out.println();
// double
System.out.println("基本类型:double 二进制位数:" + Double.SIZE);
System.out.println("最小值:" + Double.MIN_VALUE);
System.out.println("最大值:" + Double.MAX_VALUE);
System.out.println();
// char
System.out.println("基本类型:char 二进制位数:" + Character.SIZE);
System.out.println("最小值:" + (int) Character.MIN_VALUE);
System.out.println("最大值:" + (int) Character.MAX_VALUE);
}
}
测试结果:
基本类型:byte 二进制位数:8
最小值:-128
最大值:127
基本类型:short 二进制位数:16
最小值:-32768
最大值:32767
基本类型:int 二进制位数:32
最小值:-2147483648
最大值:2147483647
基本类型:long 二进制位数:64
最小值:-9223372036854775808
最大值:9223372036854775807
基本类型:float 二进制位数:32
最小值:1.4E-45
最大值:3.4028235E38
基本类型:double 二进制位数:64
最小值:4.9E-324
最大值:1.7976931348623157E308
基本类型:char 二进制位数:16
最小值:0
最大值:65535
590




被折叠的 条评论
为什么被折叠?



