Java Reference
In-Depth Information
b = -b;
assigns a negative value to the variable b .
The Assignment Operator
As you learn a programming language it is important to keep in mind that
the language expressions are not usually valid mathematically. Consider
the expression
a=a+2;
Mathematically, it is absurd to state the value of the variable a is the
same as the value that result from adding 2 to it. The reason for this ap-
parent absurdity is that Java's = operator does not represent an equality.
The = sign is used as a simple assignment operator. The result of the
statement
b=b-4;
isthatthevariable b is“assigned”thevaluethatresultsfromsubtracting4
fromitsownvalue.Inotherwords,b“becomes”b-4.Itisimportanttonote
thatthisuseofthe=signinJavaislimitedtoassigningavaluetoastorage
location referenced by a program element. For this reason a Java expres-
sion containing the = sign cannot be interpreted algebraically.
There are other differences between a Java expression and an alge-
braic equation. In elementary algebra we learn to solve an equation by
isolating a variable on the left-hand side of the = sign, as follows:
2x=y
x = y/2
However, the Java statement line
2*x=y;
generatesanerror.Thisisduetothefactthatprogramminglanguages,Java
included,arenotdesignedtoperformeventhesimplestalgebraicmanipu-
lations.
Incidentally...
Expressions in Java are usually not algebraically valid.
The two sides of an assignment
IfwelookataJavaexpressionthatusestheassignmentoperator,wenote
oneparttotheleftofthe=signandanotheronetotheright.Inanyprogram-
Search WWH ::




Custom Search