javaexpressionstatements

Statements and Expressions in Java(The statement x = y = x = 0 is illegal??)


I am studying Java and am having a problem with the following question.

Which of these statements are true?

The correct answers are b and c. However, I don't understand why those two are the answers. Can anybody explain it for me?


Solution

  • a

    Take the expression a+1 for instance. This makes no sense as a statement as it does not change anything and is not possible.

    b

    x++ returns x and increments it afterwards.

    As it changes the variable, it can be used as a statement.

    c

    When you assign a value to something else, it returns the assigned value.

    d

    This is possible because x=0 sets x to 0 and returns 0.

    Then, it sets y to x (0) and returns this value (0).

    Then, it sets x to y (0) (and returns this value (0)).