Comparison should include a summary of relational and conditional operators, shift and logical operators, and other operators, which the following tables illustrate.
Relational Operators (determine the relationship between two values):
Operator |
Use |
Returns true if |
> |
opl>op2 |
op1 is greater than op2 |
>= |
op1 >= op2 |
op1 is greater than or equal to op2 |
< |
op1 < op2 |
op1 is less than op2 |
<= |
op1 <= op2 |
op1 is less than or equal to op2 |
== |
op1 == op2 |
op1 and op2 are equal |
!= |
op1 != op2 |
op1 and op2 are not equal |
Conditional Operators (form multi-part decisions):
Operator |
Use |
Returns true if |
&& |
op1 && op2 |
op1 and op2 are both true, conditionally evaluates op2 |
|| |
op1 || op2 |
either op1 or op2 is true, conditionally evaluates op2 |
! |
! op |
op is false |
& |
op1 & op2 |
op1 and op2 are both true, always evaluates op1 and op2 |
| |
op1 | op2 |
either op1 or op2 is true, always evaluates op1 and op2 |
^ |
op1 ^ op2 |
if op1 and op2 are different—that is if one or the other of the operands is true but not both |
Shift and Logical Operators (shifts the bits of the left-hand operand over by the number of positions indicated by the right-hand operand, thereby performing logical functions on their operands):
Operator |
Use |
Operation |
>> |
op1 >> op2 |
shift bits of op1 right by distance op2 |
<< |
op1 << op2 |
shift bits of op1 left by distance op2 |
>>> |
op1 >>> op2 |
shift bits of op1 right by distance op2 (unsigned) |
Other Operators:
Operator |
Use |
Description |
?: |
op1 ? op2 : op3 |
If op1 is true, returns op2. Otherwise, returns op3. |
[] |
type [] |
Declares an array of unknown length, which contains type elements. |
[] |
type[ op1 ] |
Creates an array with op1 elements. Must be used with the new operator. |
[] |
op1[ op2 ] |
Accesses the element at op2 index within the array op1. Indices begin at zero and extend through the length of the array minus one. |
. |
op1.op2 |
Is a reference to the op2 member of op1. |
() |
op1(params) |
Declares or calls the method named op1 with the specified parameters. The list of parameters can be an empty list. The list is comma-separated. |
(type) |
(type) op1 |
Casts (converts) op1 to type. An exception will be thrown if the type of op1 is incompatible with type. |
new |
new op1 |
Creates a new object or array. op1 is either a call to a constructor or an array specification. |
instanceof |
op1 instanceof op2 |
Returns true if op1 is an instance of op2 |
& |
op1 & op2 |
bitwise and |
| |
op1 | op2 |
bitwise or |
^ |
op1 ^ op2 |
bitwise xor |
~ |
~op2 |
bitwise complement |