ITeye上“10个人,8个人不会解释这个问题”的帖子
原帖链接:http://www.iteye.com/topic/1117960
简单说一下问题,LZ要求解释下面的Java代码,x为什么最后是1。
Integer x = 1; x = x++; System.out.println(x);
不管这么说,我觉得这个问题还不错,这个帖子最后还是被隐藏了,事实上这个帖子并没有“标题党”。 不得不说ITeye上的风气是越来越浮躁了,为什么就不肯静下心来好好讨论呢,至少给出理由给出证据,而不是随手投个隐藏。
OK,进入正题。
这里表面上是关于x++的讨论,但实际上也涉及到Java中自动拆装箱的问题。
首先看《The Java Language Specification, Third Edition》中15.14.2 Postfix Increment Operator ++的描述:
A postfix expression followed by a ++ operator is a postfix increment expression. The result of the postfix expression must be a variable of a type that is convertible (§5.1.8) to a numeric type, or a compile-time error occurs. The type of the postfix increment expression is the type of the variable. The result of the postfix increment expression is not a variable, but a value.
At run time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored. The value of the postfix increment expression is the value of the variable before the new value is stored.
Note that the binary numeric promotion mentioned above may include unboxing conversion (§5.1.8) and value set conversion (§5.1.13). If necessary, value set conversion is applied to the sum prior to its being stored in the variable.
这儿明确说明了:(1)拆箱(unboxing conversion);(2)将变量的值加一的和存储回这个变量;(3)这个表达式的值为将新值存储回变量之前变量的值;(4)装箱(boxing conversion)。
用Java代码表示x=x++即:
int int_x = x.intValue(); int exprValue = int_x; int_x = int_x + 1; Integer integer_exprValue = Integer.valueOf(exprValue); x = integer_exprValue;
事实上从class文件中也可以看出:
0 iconst_1 1 invokestatic #2 <java/lang/Integer.valueOf> 4 astore_1 5 aload_1 6 astore_2 7 aload_1 8 invokevirtual #3 <java/lang/Integer.intValue> 11 iconst_1 12 iadd 13 invokestatic #2 <java/lang/Integer.valueOf> 16 dup 17 astore_1 18 astore_3 19 aload_2 20 astore_1 21 getstatic #4 <java/lang/System.out> 24 aload_1 25 invokevirtual #5 <java/io/PrintStream.println> 28 return
PS:当初发现JavaEye的时候,感叹CSDN的没落,没想到现在轮到ITeye了。不过还好,ITeye上还有RednaxelaFX,wenshao,icyfenix等大牛。衷心希望有更多的好论坛出现~ 同时求推荐一些好的技术论坛
- 关于递归删除链表节点为什么不会断链问题解释
- 关于参数传递的问题 这个帖子的感想
- 浏览器的条件注释理论,用下面一段例子来解释这个问题
- 被引用的帐户目前被锁定,可能不会登录。这个问题困扰我好久,每次都要找域管理员。
- 编写多线程程序,模拟多个人通过一个山洞。这个山洞每次只能通过一个人,每个人通过山洞的时间为2秒(sleep)。随机生成10个人,都要通过此山洞,用随机值对应的字符串表示人名,打印输出每次通过山洞的人名
- 关于CSS浮动与绝对定位的个人感悟,看完这篇文章能得到很多相关问题的解释
- localhost与127.0.0.1的区别,关键在防火墙这个问题上的解释。
- 答读者问(10):有关对博客的评价及个人技术发展路线等问题
- 显式注释会被JSP引擎解释(由于忽略了这个问题,今天调试jsp 页面时浪费了很多时间)
- 用Semaphore来模拟3个位置供10个人用的问题
- 随机数问题--已知有个Random7()的函数,返回1到7随机自然数,让利用这个Random7()构造Random10()随机1~10.
- NAS DIY的设计和实施过程-10-SMB篇-再不更新估计已经有N个人要骂我要太监这个NAS项目了
- 从多方查找资源并测试终于解决了问题,特记录下。 其实如果在安装的时候,正确的安装,就不会出现下面的信息了. 如在Linux下安装,提示密码的时候,输入Root账号的密码就不会出现这个问题了!
- 【基础备忘】关于递归删除链表节点为什么不会断链问题解释
- boj 1336 简单的问题 不过自己没想到 别人解释的~~学习~~这个是 第二类Stirling数 需要再好好看看
- 这个问题不会答,跳槽怎么成?
- 你必须知道的税务规划—个人所得税,报,还是不报?这个问题
- 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期
- new File(path)关于这个path的绝对与相对路径问题(ps部分 dos下切换盘符,user.dir解释)
- 在vmware如何安装vmware tools,遇到了问题,看了许多经验帖子,结果下面这个帖子的确帮到了我。