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

public class Test{
static{
   int x=5;
}
static int x,y;
public static void main(String args[]){
   x--;
   myMethod( );
   System.out.println(x+y+ ++x);
}
public static void myMethod( ){
  y=x++ + ++x;    //注意這個運算  ++x 已是x++ 的下一個指令了,所以,x 已經+1
 }
}
  • compiletime error
  • prints:1
  • prints:2
  • prints:3   (答案)
  • prints:7
  • prints:8
相關文章
相關標籤/搜索