Why does this Java code compile?

前端 未结 14 2047
情深已故
情深已故 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:04

    In int x = x + 1; you add 1 to x , so what is the value of x, it's not created yet.

    But in int x=x=1; will compile with no error because you assign 1 to x.

    0 讨论(0)
  • 2021-01-30 02:05

    Your first piece of code contains a second = instead of a plus. This will compile anywhere while the second piece of code will not compile in either place.

    0 讨论(0)
提交回复
热议问题