Java Reference
In-Depth Information
Table 2.4
(continued)
OPERATOR
SYNTAX
Equality
= =, ! =
Bitwise AND, OR, XOR
&, |, ^
Conditional AND, OR
&&, ||
Ternary operator
? :
Assignment operator
=
Assignment with operation
*=, /=, %=, +=, -=, <<=, >> =, >>> =,
&=, ^=, |=
The following ArithmeticDemo demonstrates some of the operators and
their order of operations. For example, notice that an int m is declared and
assigned to 15%4, read “15 modulus 4”. The modulus operator % returns the
remainder when the two integers are divided.
Study all the statements in the ArithmeticDemo program carefully and try
to determine what the output is. The actual output is shown in Figure 2.6.
public class ArithmeticDemo
{
public static void main(String [] args)
{
System.out.println(5 + 4 * 6 / 3 - 2);
System.out.println((5 + 4) * 6 / (3 - 2));
int x = 5, y, z;
y = x++;
System.out.println(“x = “ + x + “ y = “ + y);
x = 5;
z = ++x;
System.out.println(“x = “ + x + “ z = “ + z);
int m = 15%4;
System.out.println(“m = “ + m);
m = 29;
System.out.println(“m << 2 = “ + (m >> 2));
double d = 5.0;
d *= 4.0;
System.out.println(“d = “ + d);
System.out.println(“Ternary: “ + (x==5 ? “yes” : “no”));
}
}
Search WWH ::




Custom Search