Java语言提供了八种基本类型。六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型。
public class DataType { static byte b; static short s; static int i; static long l; static float f; static double d; static char c; static boolean bool; public static void main(String args[]) { System.out.println("byte[大小:"+Byte.SIZE +", 默认值:"+b +"; 数据范围:"+Byte.MIN_VALUE+" ~ "+Byte.MAX_VALUE+"]"); System.out.println("short[大小:"+Short.SIZE +", 默认值:"+s +"; 数据范围:"+Short.MIN_VALUE+" ~ "+Short.MAX_VALUE+"]"); System.out.println("int[大小:"+Integer.SIZE +", 默认值:"+i +"; 数据范围:"+Integer.MIN_VALUE+" ~ "+Integer.MAX_VALUE+"]"); System.out.println("long[大小:"+Long.SIZE +", 默认值:"+l +"; 数据范围:"+Long.MIN_VALUE+" ~ "+Long.MAX_VALUE+"]"); System.out.println("float[大小:"+Float.SIZE +", 默认值:"+f +"; 数据范围:"+Float.MIN_VALUE+" ~ "+Float.MAX_VALUE+"]"); System.out.println("double[大小:"+Double.SIZE +", 默认值:"+d +"; 数据范围:"+Double.MIN_VALUE+" ~ "+Double.MAX_VALUE+"]"); System.out.println("char[大小:"+Character.SIZE +", 默认值:"+c +"; 数据范围:"+(int)Character.MIN_VALUE+" ~ "+(int)Character.MAX_VALUE+"]"); System.out.println("boolean[默认值:"+bool +"; 数据范围:"+Boolean.TRUE+" & "+Boolean.FALSE+"]"); } }
运行结果