An assignment statement designates a value for a variable. An assignment statement can be used as an expression in Java.
After a variable is declared, you can assign a value to it by using an assignment statement. In Java, the equal sign is used as the assignment operator. The syntax for assignment statements is as follows:
An expression represents a computation involving values, variables, and operators that, when taking them together, evaluates to a value. For example, consider the following code:
You can use a variable in an expression. A variable can also be used on both sides of the operator. For example:
In the above assignment statement, the result of is assigned to the variable . Let’s say that is before the statement is executed, and so becomes after the statement execution.
To assign a value to a variable, you must place the variable name to the left of the assignment operator. Thus the following statement is wrong:
Note that the math equation ≠ the Java expression
In Java, an assignment statement is an expression that evaluates a value, which is assigned to the variable on the left side of the assignment operator. Whereas an assignment expression is the same, except it does not take into account the variable.
That’s why the following statements are legal:
Which is equivalent to:
And this statement
is equivalent to:
Note: The data type of a variable on the left must be compatible with the data type of a value on the right. For example, would be illegal, because the data type of is (integer) and does not accept the value without Type Casting.
inty=1; // Assign 1 to variable y
doubleradius=1.0; // Assign 1.0 to variable radius
intx=5*(3/2); // Assign the value of the expression to x
x=y+1; // Assign the addition of y and 1 to x
doublearea=radius *radius *3.14;// Compute the area
1=x; // Wrong
//If a value is assigned to multiple variables, you can use this syntax:
When making an assignment, the value assigned is considered the "result" or "return value" of the expression. It's subtle and maybe not clear because your example is something that is rarely done.
At this point in the code, both and are equal to 6. We assigned i to the value 6 and then captured the return value of the expression, which is the value which was transferred, which is 6.
Personally, I would avoid this because it can be very confusing and easy to introduce bugs. Especially in a System.out.println or any call that is expected to not change the state. It violates the principle of least astonishment.
Ever wonder why programming classes teach you to always use a double-equals () in an if statement? Ever noticed that with a single equal sign, it may still work? This is why. The process of evaluating a condition is changing the condition, creating a truism or falsism. The number of software defects caused by .....
Many programmers (I have not picked up the habit yet, but probably ought to) prefer to write if statements with the constant on the left side like instead of so that if they accidentally use a single equal sign, the compiler won't allow it.
To take this to your code example, you are right, the expression is "returning" a value.
When the JVM runs this code, one instruction at a time, it sees 3 instructions in this order:
answered Feb 27 '14 at 15:10