Classes/Objects  «Prev  Next»

Lesson 2 Java operators
ObjectiveSummarize the different types of operators supported by Java.

Arithmetic and string operators

The arithmetic operators are the most commonly used Java operators.
These operators consist of the unary operators, +, -, ++, and --, and the binary operators +, -, *, /, and %. The ++ and -- operators come in both prefix and postfix forms. The + operator is also used to identify concatenation between two string objects.

Bitwise/Shift operators

Java's bitwise operators consist of a single unary operator ~, and three binary operators, &, |, and ^, that perform bitwise operations on integer values. The binary shift operators are <<, >>, and >>>.

Logical/Comparison Operators

Java provides two groups of logical operators:
  1. the boolean operators !, &, |, and
  2. ^, and the logical short-circuit operators && and ||.
The logical operators are restricted to boolean operands.
The comparison operators consist of the relational operators
(<, <=, >, >=), 
the equality operators
(== and !=), 
and the instanceof operator.

Assignment operators

Java's assignment operators consist of the simple assignment (=) operator, and the update and assign operators +=, -=, *=, /=,%=, &=, |=, ^=, <<=, >>=, and >>>=.

The ternary operator

The ternary operator ? : (also referred to as the Conditional operator) has three operands and takes the following form:
operand1 ? operand2 : operand3

If the first operand (boolean) evaluates to true, the value of the second operand is returned. Otherwise, the value of the third operand is returned.

The cast operator

The cast[1] operator (type) is used to convert numeric values from one numeric type to another, or to change an object reference to a compatible type.
[1]Cast: To convert from one type to another.