Java Reference
In-Depth Information
Figure 2.7
Output of the ShiftDemo program.
public class ShiftDemo
{
public static void main(String [] args)
{
byte b = 11;
System.out.println(b << 1); //Shift to the left
System.out.println(b >> 1); //Signed shift to the right
System.out.println(b >>> 1); //Unsigned shift to the right
byte c = -10;
System.out.println(c << 3); //Shift to the left three
System.out.println(c >> 1); //Sign shift to the right
System.out.println(c >>> 1);
}
}
Comparison Operators
Table 2.5 shows the comparison operators in Java, as well as the data types that
each can be used with. Notice that the “equal to” and “not equal to” operators
can be used on both primitive data types and references, but it does not make
sense to compare references to see if one reference is less than another. (C++
programmers will be interested here to know that Java does not allow for oper-
ator overloading.)
Table 2.5
Comparison Operators
OPERATOR
SYNTAX
VALID DATA TYPES
Less than
<
byte, short, int, long, float, double, char
Less than or equal to
<=
byte, short, int, long, float, double, char
Greater than
>
byte, short, int, long, float, double, char
Greater than or equal to
>=
byte, short, int, long, float, double, char
Search WWH ::




Custom Search