java 基本数据类型

该列表摘自 thinking in java 第四版 23页:


基本数据类型大小最小值最大值包装器类型
boolean---Boolean
char16-bitUnicode 0Unicode 2^16 -1Character
byte8-bit-128+127Byte
short16-bit-2^152^15 -1Shoft
int32bit-2^312^31 -1Integer
long64bit-2^632^63 -1Long
float32bitIEEE754IEEE754Float
double64bitIEEE754IEEE754Double
void---Void


byte最大值

在java中有符号整数,首位数值0和1分别表示该数为正数和负数。以byte的最大值127为例打印二进制,代码为:

public static void main(String[] args) {
       System.out.println("Integer.toBinaryString(127) : "+Integer.toBinaryString(127));
}
输出结果为:

Integer.toBinaryString(127) : 1111111

在java中byte的最大位数为8位(一个字节),为了包含该位数所能包含的数值并有正负之分故而保留了一位作为正负标识。所以正整数的最大值为127,用二进制表示为01111111。故而byte的正整数最大值为2的7次方减去1。


以下是将int值转换为二进制表示的方法,来至:IntegralToString( 由Integer.toBinaryString方法调用,该类被@hide)

 public static String intToBinaryString(int i) {
        int bufLen = 32;  // Max number of binary digits in an int
        char[] buf = new char[bufLen];
        int cursor = bufLen;

        do {
            buf[--cursor] = DIGITS[i & 1];
        }  while ((i >>>= 1) != 0);

        return new String(cursor, bufLen - cursor, buf);
    }

int类型的为32位,输出值应为:0000000000000000000000001111111  。而上面的方法每次循环的判断条件为是向右位移且结果不等于0,导致其余位数没有进行输出。


补码

操作步骤为 先取反加1

以int类型数值8为例,8的二进制为

00000000 00000000 00001000

首先取反

11111111 11111111 11110111

然后进行补码(+1,逢二进一),得到的值为

11111111 11111111 11111000


打印一下正负128的二进制:

Integer.toBinaryString(128) : 10000000
Integer.toBinaryString(-128) : 11111111111111111111111110000000


首先取反

11111111 11111111 11111111 011111111

然后进行补码

11111111 11111111 11111111 10000000

关于补码

http://baike.baidu.com/link?url=B9Q-gYM8bfHlR2IC4A_ILO2M6AqdgPL0Y-ChNU9kxuGMsN9Trx96n8oc5Zr4dd4EmqiptoOGJCdgbU-zQVydVa






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值