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

 

 

 

 

 

 

 

 

代码如下:

 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

 

转载于:https://www.cnblogs.com/creasylai19/p/3807232.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值