基本数据类型
ps:一个Byte(字节)等于8个bit(位),bit是最小的单位,1B(字节)=8bit(位)。
延伸:一般情况下,一个汉字是两个字节,英文与数字是一个字节。
打印
/**
* java基本数据类型获取字节数Byte
* <p>
* Java中存在多个基本数据类型,如:int、float、double、short等。
* 在实际应用中,我们有可能需要获取这些类型的字节数,获取数据类型的字节数可以通过直接写的方式,
* 因为java中基本数据类型的长度是固定的。这里介绍另一种方式。
*/
public class Test {
public static void main(String[] args) {
int byteSize = Byte.SIZE;
System.out.println(" byte size: " + (byteSize / 8) + "Byte");
// byte size: 1Byte
int shortSize = Short.SIZE;
System.out.println(" short size: " + (shortSize / 8) + "Byte");
// short size: 2Byte
int intSize = Integer.SIZE;
System.out.println(" int size: " + (intSize / 8) + "Byte");
// int size: 4Byte
int longSize = Long.SIZE;
System.out.println(" long size: " + (longSize / 8) + "Byte");
// long size: 8Byte
int floatSize = Float.SIZE;
System.out.println(" float size: " + (floatSize / 8) + "Byte");
// float size: 4Byte
int doubleSize = Double.SIZE;
System.out.println(" double size: " + (doubleSize / 8) + "Byte");
// double size: 8Byte
int byteBytes = Byte.BYTES;
System.out.println("byteBytes = " + byteBytes);
//byteBytes = 1
int shortBytes = Short.BYTES;
System.out.println("shortBytes = " + shortBytes);
//shortBytes = 2
int integerBytes = Integer.BYTES;
System.out.println("integerBytes = " + integerBytes);
//integerBytes = 4
int longBytes = Long.BYTES;
System.out.println("longBytes = " + longBytes);
//longBytes = 8
int floatBytes = Float.BYTES;
System.out.println("floatBytes = " + floatBytes);
//floatBytes = 4
int doubleBytes = Double.BYTES;
System.out.println("doubleBytes = " + doubleBytes);
//doubleBytes = 8
}
}