What will happen when you attempt to compile and run the following code?

淩亂°似流年 2022-10-02 00:36 87阅读 0赞

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

  1. public class Test{
  2. static{
  3. int x=5;
  4. }
  5. static int x,y;
  6. public static void main(String args[]){
  7. x--;
  8. myMethod( );
  9. System.out.println(x+y+ ++x);
  10. }
  11. public static void myMethod( ){
  12. y=x++ + ++x; //注意这个运算 ++x 已经是x++ 的下一个指令了,因此,x 已经+1
  13. }
  14. }
  • compiletime error
  • prints:1
  • prints:2
  • prints:3 (答案)
  • prints:7
  • prints:8

转载于:https://my.oschina.net/u/3847203/blog/3015476

发表评论

表情:
评论列表 (有 0 条评论,87人围观)

还没有评论,来说两句吧...

相关阅读