Java获取基本类型的字节数

Java中存在多个基本数据类型,如:int、float、double、short等。在实际应用中,我们有可能需要获取这些类型的字节数,获取数据类型的字节数可以通过直接写的方式,因为java中基本数据类型的长度是固定的。这里介绍另一种方式。

 

我们可以使用基本类型的封装类型的SIZE常量,该常量用来以二进制补码形式表示基本数据类型值的比特位数。

package com.bug315;

 

public class IntTest {

 

    public static void main(String[] args) {

        int intSize = Integer.SIZE;

        System.out.println("    int size: " + (intSize/8) + "Byte" );

         

        int shortSize = Short.SIZE;

        System.out.println("  short size: " + (shortSize/8) + "Byte" );

         

        int longSize = Long.SIZE;

        System.out.println("   long size: " + (longSize/8) + "Byte" );

         

        int byteSize = Byte.SIZE;

        System.out.println("   byte size: " + (byteSize/8) + "Byte" );

         

        int floatSize = Float.SIZE;

        System.out.println("  float size: " + (floatSize/8) + "Byte" );

         

        int doubleSize = Double.SIZE;

        System.out.println(" double size: " + (doubleSize/8) + "Byte" );

    }

     

}

输出结果:

    int size: 4Byte

  short size: 2Byte

   long size: 8Byte

   byte size: 1Byte

  float size: 4Byte

 double size: 8Byte

 

来源:http://www.hxstrive.com/article/218.htm

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值