Java Reference
In-Depth Information
1.4.1 assignment operators
A simple Java program that illustrates a few operators is shown in Figure 1.3.
The basic assignment operator is the equals sign. For example, on line 16 the
variable a is assigned the value of the variable c (which at that point is 6). Sub-
sequent changes to the value of c do not affect a . Assignment operators can be
chained, as in z=y=x=0 .
Another assignment operator is the += , whose use is illustrated on line 18
of the figure. The += operator adds the value on the right-hand side (of the +=
operator) to the variable on the left-hand side. Thus, in the figure, c is incre-
mented from its value of 6 before line 18, to a value of 14.
Java provides various other assignment operators, such as -= , *= , and /= ,
which alter the variable on the left-hand side of the operator via subtraction,
multiplication, and division, respectively.
Java provides a
host of assignment
operators , includ-
ing = , += ,
-= , *= , and /= .
1 public class OperatorTest
2 {
3 // Program to illustrate basic operators
4 // The output is as follows:
5 // 12 8 6
6 // 6 8 6
7 // 6 8 14
8 // 22 8 14
9 // 24 10 33
10
11 public static void main( String [ ] args )
12 {
13 int a = 12, b = 8, c = 6;
14
15 System.out.println( a + " " + b + " " + c );
16 a = c;
17 System.out.println( a + " " + b + " " + c );
18 c += b;
19 System.out.println( a + " " + b + " " + c );
20 a = b + c;
21 System.out.println( a + " " + b + " " + c );
22 a++;
23 ++b;
24 c = a++ + ++b;
25 System.out.println( a + " " + b + " " + c );
26 }
27 }
figure 1.3
Program that
illustrates operators
 
Search WWH ::




Custom Search