字节解惑

2018-12-02  本文已影响0人  ArgusK

写在前面

字节含义有以下2种:

  • 表示计算机存储容量的计量单位(B), 1KB=1024B
  • 表示一些计算机编程语言中的数据类型(byte)

字符、字节和编码

ASCII:1个英文字母=1B, 1个汉字=2B

UTF-8:1个英文字母=1B, 1个汉字=3B

Unicode:1个英文字母=2B, 1个汉字=2B

例如:vi test.txt,输入以下内容

abc中国

File file = new File("test.txt");
System.out.println(file.length());//输出9

数据类型

数据类型 内存占用空间
byte 1字节(8bit)
short 2字节
char 2字节
int 4字节
float 4字节
long 8字节
double 8字节

测试代码

System.out.println("Byte: " + Byte.SIZE);
System.out.println("Short: " + Short.SIZE);
System.out.println("Character: " + Character.SIZE);
System.out.println("Integer: " + Integer.SIZE);
System.out.println("Float: " + Float.SIZE);
System.out.println("Long: " + Long.SIZE);
System.out.println("Double: " + Double.SIZE);

输出如下

Byte: 8
Short: 16
Character: 16
Integer: 32
Float: 32
Long: 64
Double: 64

上一篇 下一篇

猜你喜欢

热点阅读