牛客网基础题-有容乃大

牛客网基础题-有容乃大

确定不同数据类型在内存中占多少字节
思路:

  1. Java的基本数据类型有8种,分别是:boolean,char,byte,short,int,long,float,double.java是一个面向对象的语言,它的操作一般都是基于对象,因此为了使用方便将它们当成对象来操作,为每种类型均引入了其对应的包装类型。其中,包装类型属于java.lang.*;
基本数据类型包装类型所占空间大小(字节数)
booleanBoolean-
charCharacter2
byteByte1
shortShort2
intInteger4
longLong8
floatFloat4
doubleDouble8
  1. 这些包装类型封装了一些常用的方法和属性。本题便要使用它们的SIZE属性。使用SIZE可以计算出实际所占空间大小
    代码如下:
public class Main {
    public static void main(String[] args){
        System.out.println("The size of short is "+Short.SIZE/Byte.SIZE+" bytes.");
        System.out.println("The size of int is "+ Integer.SIZE/Byte.SIZE+" bytes.");
        System.out.println("The size of long is "+Long.SIZE/Byte.SIZE+" bytes.");
        System.out.println("The size of long long is "+Long.SIZE/Byte.SIZE+" bytes.");
    }
}

运行结果:

The size of short is 2 bytes.
The size of int is 4 bytes.
The size of long is 8 bytes.
The size of long long is 8 bytes.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值