一道关于Java的题目
高洛峰
高洛峰 2017-04-18 09:05:54
0
2
374

我实在不懂为什么是-5536

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
伊谢尔伦

Simplify

In fact, this question can be simplified and directly

Console.Write("{0}",(short)60000);

Underlying reasons

When a variable is defined as short型的时候,其全称是有符号短整数型,这种类型占用2个字节,即word型,其范围是0x00000xffff,其中0x00000x7fff的最高为0,代表其为正数,0x7fff0xffff最高为1,代表其为负数。
ok,那么十进制的60000,即十六进制的EA60, it is obviously a negative number, and it is originally a negative number.

Expand

If the type here is changed to unsigned short,那么输出的结果依然为60000.

Peter_Zhu

Memory out of bounds

In Java, the size range of short is: -32 768 to 32 767. Because your Add result 60000 is obviously out of bounds (32767), it will cause an error when casting to short type. It should be changed to

int answer = (int) Add(num1,num2);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template