intiValue=233;bytebValue=(byte)iValue;//int强制转化为byteSystem.out.println(bValue);如上,输出结果是什么?为什么?
2 回答

沧海一幻觉
TA贡献1824条经验 获得超5个赞
计算机中数值都是以二进制补码的形式存储二进制数值的第一位代表数值的符号(正数为0,负数为1)其中正数的补码等于其原码负数的补码等于其原码(除符号位)的反码+1java中int占32位byte占8位int233=00000000000000000000000011101001强转后去掉前面的24个0,得11101001补充:补码转换为原码的规则:对该补码再次求补码接着判断首位是1,为负数,取补码得10010110+1=>-23

九州编程
TA贡献1785条经验 获得超4个赞
楼上正确,补一下官方文档:jls7,5.1.3NarrowingPrimitiveConversionAnarrowingconversionofasignedintegertoanintegraltypeTsimplydiscardsallbutthenlowestorderbits,wherenisthenumberofbitsusedtorepresenttypeT.Inadditiontoapossiblelossofinformationaboutthemagnitudeofthenumericvalue,thismaycausethesignoftheresultingvaluetodifferfromthesignoftheinputvalue.
添加回答
举报
0/150
提交
取消