Lesson 2 | Java operators |
Objective | Summarize the different types of operators supported by Java. |
Different types of Java 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.
Java Arithmetic Operators
Operator | Description | Example | Result |
+ (Unary) | Sign identification | +1 | 1 |
+ (Binary) | Addition | 2 + 2 | 4 |
- (Unary) | Negation | -x | The sign of x is changed. |
- (Binary) | Subtraction | 3 - 2 | 1 |
++ (Prefix) | Increment and then return the new value. | x=1;
y=++x; | The value of x is 2 and the value of y is 2 . |
++ (Postfix) | Return the current value and then increment. | x=1;
y=x++; | The value of x is 2 and the value of y is 1 . |
-- (Prefix) | Decrement and then return the new value. | x=1;
y=--x; | The value of x is 0 and the value of y is 0 . |
-- (Postfix) | Return the current value and then decrement. | x=1;
y=x--; | The value of x is 0 and the value of y is 1 . |
* | Multiplication. | 4 * 4 | 16 |
/ | Division. Note that rounding towards zero is used with integer types. | 19 / 4 | 4 |
% | Remainder. Take the remainder after division. The sign of the operand determines the sign of the result. | 19 % 4 | 3 |
+ (String) | String concatenation. | "abc" + "def" | "abcdef" |
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 >>>
.
Java Bitwise Operators
Operator | Description | Example | Result | Computation |
~ | Bitwise complement. | ~3 | -4 | ~3=~(00000011)=(11111100)=-4 |
& | Bitwise AND | 3 & 5 | 1 | 3 & 5=(00000011) & (00000101)=(00000001)=1 |
| | Bitwise OR | 3 | 4 | 7 | 3 | 4=(00000011) | (00000100)=(00000111)=7 |
^ | Bitwise exclusive OR | 5 ^ 4 | 1 | 5 ^ 4=(00000101) ^ (00000100)=(00000001)=1 |
<< | Left shift. | 3 << 3 | 24 | 3 << 3=(00000011) << 3=(00011000)=24 |
>> | Signed right shift | -6 >> 2 | -2 | -6 >> 2=(11111000) >> 2=(11111110)=-2 |
>>> | Unsigned right shift. | 6 >>> 2 | 1 | 6 >>> 2=(00000110) >>> 2=(00000001)=1 |
public class BitwiseOperators {
public static void main(String args[]) {
System.out.println(~3 );
System.out.println(3 & 5);
System.out.println(3 | 4);
System.out.println(5 ^ 4);
System.out.println(3 << 3);
System.out.println(-6 >> 2);
System.out.println(6 >>> 2);
}
}
Program Output
-4
1
7
1
24
-2
1
Logical Operators
The exam objectives specify six "logical" operators (&, |, ^, !, &&, and ||).
Some Oracle documentation uses other terminology for these operators, but for our purposes and in the exam objectives, these six are the logical operators.
Bitwise Operators
Of the six logical operators listed above, three of them (&, |, and ^) can also be used as "bitwise" operators. Bitwise operators
were included in previous versions of the exam, but they are NOT on the Java 6 or Java 7 exam.
Here are several legal statements that use bitwise operators:
byte b1 = 6 & 8;
byte b2 = 7 | 9;
byte b3 = 5 ^ 4;
System.out.println(b1 + " " + b2 + " " + b3);
Bitwise operators compare two variables bit-by-bit and return a variable whose bits have been set based on whether the two variables being compared had respective bits that were either both "on" (&), one or the other "on" (|), or exactly one "on" (^). By the way, when we run the preceding code, we get
0 15 1
Logical/Comparison Operators
Java provides two groups of
logical operators:
- the boolean operators
!
, &
, |
, and
^
, 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.