java基本数据类型获取字节数Byte

基本数据类型

在这里插入图片描述

ps:一个Byte(字节)等于8个bit(位),bit是最小的单位,1B(字节)=8bit(位)。
延伸:一般情况下,一个汉字是两个字节,英文与数字是一个字节。

打印


/**
 * java基本数据类型获取字节数Byte
 * <p>
 * Java中存在多个基本数据类型,如:int、float、double、short等。
 * 在实际应用中,我们有可能需要获取这些类型的字节数,获取数据类型的字节数可以通过直接写的方式,
 * 因为java中基本数据类型的长度是固定的。这里介绍另一种方式。
 */
public class Test {
    public static void main(String[] args) {
        int byteSize = Byte.SIZE;
        System.out.println("   byte size: " + (byteSize / 8) + "Byte");
        //   byte size: 1Byte
        int shortSize = Short.SIZE;
        System.out.println("  short size: " + (shortSize / 8) + "Byte");
        //  short size: 2Byte
        int intSize = Integer.SIZE;
        System.out.println("    int size: " + (intSize / 8) + "Byte");
        //    int size: 4Byte
        int longSize = Long.SIZE;
        System.out.println("   long size: " + (longSize / 8) + "Byte");
        //   long size: 8Byte
        int floatSize = Float.SIZE;
        System.out.println("  float size: " + (floatSize / 8) + "Byte");
        //  float size: 4Byte
        int doubleSize = Double.SIZE;
        System.out.println(" double size: " + (doubleSize / 8) + "Byte");
        // double size: 8Byte

        int byteBytes = Byte.BYTES;
        System.out.println("byteBytes = " + byteBytes);
        //byteBytes = 1
        int shortBytes = Short.BYTES;
        System.out.println("shortBytes = " + shortBytes);
        //shortBytes = 2
        int integerBytes = Integer.BYTES;
        System.out.println("integerBytes = " + integerBytes);
        //integerBytes = 4
        int longBytes = Long.BYTES;
        System.out.println("longBytes = " + longBytes);
        //longBytes = 8
        int floatBytes = Float.BYTES;
        System.out.println("floatBytes = " + floatBytes);
        //floatBytes = 4
        int doubleBytes = Double.BYTES;
        System.out.println("doubleBytes = " + doubleBytes);
        //doubleBytes = 8
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值