Why does this Java code compile?

前端 未结 14 2264
情深已故
情深已故 2021-01-30 01:24

In method or class scope, the line below compiles (with warning):

int x = x = 1;

In class scope, where variables get their default valu

14条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-30 02:03

    The second one int x=x=1 is compile because you are assigning the value to the x but in other case int x=x+1 here the variable x is not initialized , Remember in java local variable are not initialized to default value. Note If it's (int x=x+1) in class scope also then also it will give compilation error as the variable is not created.

提交回复
热议问题