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

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

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 Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char 'u0000'
String (or any object)   null
boolean false

 

 

 

 

 

 

 

 

代码如下:

 1 package com.example.li;
 2 
 3 public class TestBaseType {
 4 
 5     public static void main(String[] args) {
 6         System.out.println("Byte.SIZE__" + Byte.SIZE / 8);// 1字节
 7         System.out.println("Short.SIZE__" + Short.SIZE / 8);// 2字节
 8         System.out.println("Integer.SIZE__" + Integer.SIZE / 8);// 4字节
 9         System.out.println("Long.SIZE__" + Long.SIZE / 8);// 8字节
10         System.out.println("Character.SIZE__" + Character.SIZE / 8);// 2字节
11         System.out.println("Float.SIZE__" + Float.SIZE / 8);// 4字节
12         System.out.println("Double.SIZE__" + Double.SIZE / 8);// 8字节
13         // System.out.println(Boolean.SIZE);
14     }
15 
16 }

运行结果如下:

1 Byte.SIZE__1
2 Short.SIZE__2
3 Integer.SIZE__4
4 Long.SIZE__8
5 Character.SIZE__2
6 Float.SIZE__4
7 Double.SIZE__8