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 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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值