Why does this Java code compile?

前端 未结 14 2018
情深已故
情深已故 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 01:44
    int x = x + 1;
    

    compiles successfully in Visual Studio 2008 with warning

    warning C4700: uninitialized local variable 'x' used`
    
    0 讨论(0)
  • 2021-01-30 01:45

    Let's break it down step by step, right associative

    int x = x = 1
    

    x = 1, assign 1 to a variable x

    int x = x, assign what x is to itself, as an int. Since x was previously assigned as 1, it retains 1, albeit in a redundant fashion.

    That compiles fine.

    int x = x + 1
    

    x + 1, add one to a variable x. However, x being undefined this will cause a compile error.

    int x = x + 1, thus this line compile errors as the right portion of the equals will not compile adding one to an unassigned variable

    0 讨论(0)
  • 2021-01-30 01:50
    int x = x = 1;
    

    is equivalent to

    int x = 1;
    x = x; //warning here
    

    while in

    int x = x + 1; 
    

    first we need to compute x+1 but the value of x is not known so you get an error (the compiler knows that the value of x is not known)

    0 讨论(0)
  • 2021-01-30 01:53

    In the second piece of code, x is used before its declaration, while in the the first piece of code it is just assigned twice which doesn't make sense but is valid.

    0 讨论(0)
  • 2021-01-30 01:57

    It's roughly equivalent to:

    int x;
    x = 1;
    x = 1;
    

    Firstly, int <var> = <expression>; is always equivalent to

    int <var>;
    <var> = <expression>;
    

    In this case, your expression is x = 1, which is also a statement. x = 1 is a valid statement, since the var x has already been declared. It is also an expression with the value 1, which is then assigned to x again.

    0 讨论(0)
  • 2021-01-30 01:57

    int x = x = 1; is not equal to:

    int x;
    x = 1;
    x = x;
    

    javap helps us again, these are JVM instruction generated for this code:

    0: iconst_1    //load constant to stack
    1: dup         //duplicate it
    2: istore_1    //set x to constant
    3: istore_1    //set x to constant
    

    more like:

    int x = 1;
    x = 1;
    

    Here is no reason to throw undefined reference error. There is now usage of variable prior to it's initialization, so this code fully comply with specification. In fact there is no usage of variable at all, just assignments. And JIT compiler will go even further, it will eliminate such constructions. Saying honestly, I don't understand how this code is connected to JLS's specification of variable initialization and usage. No usage no problems. ;)

    Please correct if I'am wrong. I cant figure out why other answers, which refer to many JLS paragraphs collect so many pluses. These paragraphs has nothing in common with this case. Just two serial assignments and no more.

    If we write:

    int b, c, d, e, f;
    int a = b = c = d = e = f = 5;
    

    is equal to:

    f = 5
    e = 5
    d = 5
    c = 5
    b = 5
    a = 5
    

    Right most expression is just assigned to variables one by one, without any recursion. We can mess variables any way we like :

    a = b = c = f = e = d = a = a = a = a = a = e = f = 5;
    
    0 讨论(0)
提交回复
热议问题