java有三种基本数据类型:数值型、字符型、布尔型。
数值型又分为:byte、short、int、long、float、double
字符型为:char
布尔型为:boolean
下面代码输出这几种数据类型的取值范围以及所占据的空间
public class data {
public static void main (String []args) {
System.out.println("BYTE: ");
System.out.println("包装类: java.lang.Byte");
System.out.print("byte size: "+Byte.SIZE+" bits"+"\n");
System.out.print("byte size: "+Byte.BYTES+" Bytes"+"\n");
System.out.print("byte MAX_VALUE: "+Byte.MAX_VALUE+"\n");
System.out.print("byte MIN_VALUE: "+Byte.MIN_VALUE+"\n");
System.out.println("SHORT: ");
System.out.println("包装类: java.lang.Short");
System.out.print("short size: "+Short.SIZE+" bits"+"\n");
System.out.print("short size: "+Short.BYTES+" Bytes"+"\n");
System.out.print("short MAX_VALUE: "+Short.MAX_VALUE+"\n");
System.out.print("short MIN_VALUE: "+Short.MIN_VALUE+"\n");
System.out.println("Int: ");
System.out.println("包装类: java.lang.Integer");
System.out.print("int size: "+Integer.SIZE+ " bits"+"\n");
System.out.print("int size: "+Integer.BYTES+ " Bytes"+"\n");
System.out.print("int MAX_VALUE: "+Integer.MAX_VALUE+"\n");
System.out.print("int MIN_VALUE: "+Integer.MIN_VALUE+"\n");
System.out.println("LONG: ");
System.out.println("包装类: java.lang.Long");
System.out.print("Long size: "+Long.SIZE+ " bits"+"\n");
System.out.print("Long size: "+Long.BYTES+ " Bytes"+"\n");
System.out.print("Long MAX_VALUE: "+Long.MAX_VALUE+"\n");
System.out.print("Long MIN_VALUE: "+Long.MIN_VALUE+"\n");
System.out.println("FLOAT: ");
System.out.println("包装类: java.lang.Float");
System.out.print("Float size: "+Float.SIZE+ " bits"+"\n");
System.out.print("Float size: "+Float.BYTES+ " Bytes"+"\n");
System.out.print("Float MAX_VALUE: "+Float.MAX_VALUE+"\n");
//这里有问题??最小值不应该是-3.403e38?
System.out.println("DOUBLE: ");
System.out.println("包装类: java.lang.Double");
System.out.print("Double size: "+Double.SIZE+ " bits"+"\n");
System.out.print("Double size: "+Double.BYTES+ " Bytes"+"\n");
System.out.print("Double MAX_VALUE: "+Double.MAX_VALUE+"\n");
System.out.print("Double MIN_VALUE: "+Double.MIN_VALUE+"\n");
//这里有问题??最小值不应该是-1.7976931348623157E308
System.out.println("CHAR: ");
System.out.println("包装类: java.lang.Character");
System.out.print("Character size: "+Character.SIZE+ " bits"+"\n");
System.out.print("Character size: "+Character.BYTES+ " Bytes"+"\n");
System.out.print("Character MAX_VALUE: "+(int)Character.MAX_VALUE+"\n");
System.out.print("Character MIN_VALUE: "+(int)Character.MIN_VALUE+"\n");
System.out.println("BOOLEAN: ");
System.out.println("包装类: java.lang.Boolean");
System.out.print("Boolean TURE: "+Boolean.TRUE+ "\n");
System.out.print("Boolean FALSE: "+Boolean.FALSE+ "\n");
System.out.print("Boolean TYPE: "+Boolean.TYPE+ "\n");
System.out.println("基本数据类型的定义: ");
byte b=127;
short s=32767;
int i=1234567;
long l=666666L;
float f1=1.256F;
// float f2=1.2; 错误定义,1.256为double类型数据
//赋值给float类型属于向下转换,会损失精度,编译不允许(貌似带小数均默认为double类型)
double d1=2.255D;
double d2=2.25; //这样定义没有问题,因为2.25本来就为double类型数据
char ch='\u0041'; // \u0041为A的unicode,字符统一编码
boolean flag=true;
System.out.println("byte b=" + b);
System.out.println("short s=" + s);
System.out.println("int i=" + i);
System.out.println("long l=" + l);
System.out.println("float f1=" + f1);
System.out.println("double d1=" + d1);
System.out.println("double d2=" + d2);
System.out.println("char ch=" + ch);
System.out.println("boolean flag=" + flag);
}
}
/*
结果为:
BYTE:
包装类: java.lang.Byte
byte size: 8 bits
byte size: 1 Bytes
byte MAX_VALUE: 127
byte MIN_VALUE: -128
SHORT:
包装类: java.lang.Short
short size: 16 bits
short size: 2 Bytes
short MAX_VALUE: 32767
short MIN_VALUE: -32768
Int:
包装类: java.lang.Integer
int size: 32 bits
int size: 4 Bytes
int MAX_VALUE: 2147483647
int MIN_VALUE: -2147483648
LONG:
包装类: java.lang.Long
Long size: 64 bits
Long size: 8 Bytes
Long MAX_VALUE: 9223372036854775807
Long MIN_VALUE: -9223372036854775808
FLOAT:
包装类: java.lang.Float
Float size: 32 bits
Float size: 4 Bytes
Float MAX_VALUE: 3.4028235E38
Float MIN_VALUE: 1.4E-45
DOUBLE:
包装类: java.lang.Double
Double size: 64 bits
Double size: 8 Bytes
Double MAX_VALUE: 1.7976931348623157E308
Double MIN_VALUE: 4.9E-324
CHAR:
包装类: java.lang.Character
Character size: 16 bits
Character size: 2 Bytes
Character MAX_VALUE: 65535
Character MIN_VALUE: 0
BOOLEAN:
包装类: java.lang.Boolean
Boolean TURE: true
Boolean FALSE: false
Boolean TYPE: boolean
基本数据类型的定义:
byte b=127
short s=32767
int i=1234567
long l=666666
float f1=1.256
double d1=2.255
double d2=2.25
char ch=A
boolean flag=true
*/