您的位置:首页 > 编程语言 > Java开发

Java中的基本数据类型在内存所占字节

2014-06-25 00:23 288 查看
Java中的基本数据类型分四类八种

byte(Byte-1)/short(Short-2)/int(Integer-4)/long(Long-8)

boolean(Boolean-1bit)

char(Character-2)

float(Float-4)/double(Double-8)

括号后是他们的包装类和所占字节大小(Java中的基本数据类型所占字节大小是固定的,和C/C++中不一样)

基本数据类型的默认值:

Data TypeDefault Value (for fields)
byte0
short0
int0
long0L
float0.0f
double0.0d
char'\u0000'
String (or any object) null
booleanfalse
代码如下:

package com.example.li;

public class TestBaseType {

public static void main(String[] args) {
System.out.println("Byte.SIZE__" + Byte.SIZE / 8);// 1字节
System.out.println("Short.SIZE__" + Short.SIZE / 8);// 2字节
System.out.println("Integer.SIZE__" + Integer.SIZE / 8);// 4字节
System.out.println("Long.SIZE__" + Long.SIZE / 8);// 8字节
System.out.println("Character.SIZE__" + Character.SIZE / 8);// 2字节
System.out.println("Float.SIZE__" + Float.SIZE / 8);// 4字节
System.out.println("Double.SIZE__" + Double.SIZE / 8);// 8字节
// System.out.println(Boolean.SIZE);
}

}


运行结果如下:

Byte.SIZE__1
Short.SIZE__2
Integer.SIZE__4
Long.SIZE__8
Character.SIZE__2
Float.SIZE__4
Double.SIZE__8
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: