基本数据类型
类型 | 长度 | 范围 |
---|---|---|
byte(字节型) | 1个字节 | -128~127 |
boolean(布尔型) | 1个字节 | true或false |
int(整型) | 长度:4个字节 | -2147483648~2147483647 |
short(短整型) | 2个字节 | -32768~32767 |
long(长整型) | 8 个字节 | -9223372036854775808 ~ 9223372036854775807 |
char(字符型) | 2个字节 | 从字符型对应的整型数来划分,其表示范围是0~65535 |
float(浮点型) | 4 个字节 | -3.4E38~3.4E38 |
double(双精度型) | 8个字节 | -1.7E308~1.7E308 |
注意
一个非汉字字符占用一个字节。
汉字占的字节数,取决于其编码。
任何字符的空间长度都为1。
public class Byteslength {
public static void main(String[] args) throws IOException {
String a = "中国";
String b = "a!@#$%^&*()-+";
System.out.println("----汉字----");
System.out.println(a.getBytes().length);
System.out.println(a.getBytes("GBK").length);
System.out.println(a.getBytes("GB2312").length);
System.out.println(a.getBytes("UTF-8").length);
System.out.println("----字符----");
System.out.println(b.getBytes().length);
System.out.println(b.getBytes("GBK").length);
System.out.println(b.getBytes("UTF-8").length);
System.out.println(b.getBytes("GB2312").length);
}
}