see also operators
■ Multiple assignments, which run from right to left, are OK. i.e.
int a,b,c,d;
int x = 2;
a = b = c = d = x/2; Here
a,
b,
c and d all become 1;
■ Multiple assignments are always evaluated from left to right, but with all variables taking on the end (right-hand) value. i.e.
In myarray[
a + d ] = a = b = c = d = n/2; due to the left-to-right evaluation
order, the original values of the variables [ a + d ] would
be used for the initial array indexing.
They wouldn't be changed by the statement until later, after they were
used to locate the array element myarray[ a + d ].
■ Since conditional
operators have higher precedence than assignment operators, the following
mix works: if ( boolean = int
== int ) i.e.
boolean
b = false;
int x = 2;
int y = 2;
if ( b = x == y ) returns
true while assigning true to b.
■ Assigning something using one equals sign, in an otherwise-boolean-looking expression which should have two = = signs, just returns the actual result of the assignment. It doesn't return a true boolean result. i.e.
String
a ="A";
String b = "B";
System.out.print( a = b ); prints B but
System.out.print( a == b ); will print false.
■ Direct primitive boolean variable assignments can be made using the single equals sign ( = ).
They make the assignment and
then they always return the right
hand boolean value. i.e.
(
anybooleantrue = anybooleantrue ) always returns true
(
anybooleanfalse = anybooleanfalse ) always returns false
(
anybooleantrue = anybooleanfalse ) always returns false
(
anybooleanfalse = anybooleantrue ) always returns true
(
anybooleantrue = true ) always
returns true
(
anybooleantrue = false ) always
returns false
(
anybooleanfalse = true ) always
returns true
(
anybooleanfalse = false ) always
returns false
■ You cannot use assignment against the final primitive values true and false themselves. i.e.
( true = true ) or (
true = anybooleanfalse ) will
not compile.