Java operators perform arithmetic, compare values, test conditions, and assign results.
A simple example
Test two conditions
Start withn is 2
Do thisGreater than 0 and less than 3?
Resultyes
Both comparisons succeed, so the program prints yes.Main.java
class Main {
public static void main(String[] args) throws Exception {
int n = 2;
if (n > 0 && n < 3) {
System.out.println("yes");
}
}
}
Output
yes
Look up a shelf safely
A stock scanner receives a shelf position before it knows whether that shelf exists. Check the bounds first. Only an existing shelf with at least five units can supply the order.
Types of Operators
Unary operator
Arithmetic operator
Relational operator
Logical operator
Bitwise operator
Assignment operator
Ternary operator
Java Unary Operator
A unary operator acts on one operand. Prefix and postfix increment both update the variable but yield different expression values.
Name
Operator
Description
Prefix
++exp
Increments the operand and yields the new value.
--exp
Decrements the operand and yields the new value.
Postfix
exp++
Increments the operand and yields the previous value.
exp--
Decrements the operand and yields the previous value.
Example: #1 - Prefix operator
Compare the variables before and after prefix increment or decrement.
Java source code
class Operators {
public static void main(String[] args) {
// PREFIX
System.out.println("Prefix Increment");
int i = 6;
System.out.println("The value of i before prefix increment is " + i);
int j = ++i; // increment the value by 1 and assign
System.out.println("The value of i after prefix increment is " + i);
System.out.println("The value of j is " + j);
System.out.println("Prefix Decrement");
int x = 3;
System.out.println("The value of x before prefix decrement is " + x);
int y = --x; // decrement the value by 1 and assign
System.out.println("The value of x after prefix decrement is " + x);
System.out.println("The value of y is " + y);
}
}
Output
Prefix Increment
The value of i before prefix increment is 6
The value of i after prefix increment is 7
The value of j is 7
Prefix Decrement
The value of x before prefix decrement is 3
The value of x after prefix decrement is 2
The value of y is 2
Advertisements
Example: #2 - Postfix operator
Compare the values returned by postfix increment and decrement with the updated variables.
Java source code
class Operators {
public static void main(String[] args) {
// POSTFIX
System.out.println("Postfix Increment");
int i = 6;
System.out.println("The value of i before postfix increment is " + i);
int j = i++; // assign and increment the value by 1
System.out.println("The value of i after postfix increment is " + i);
System.out.println("The value of j is " + j);
System.out.println("Postfix Decrement");
int x = 3;
System.out.println("The value of x before postfix decrement is " + x);
int y = x--; // assign and decrement the value by 1
System.out.println("The value of x after postfix decrement is " + x);
System.out.println("The value of y is " + y);
}
}
Output
Postfix Increment
The value of i before postfix increment is 6
The value of i after postfix increment is 7
The value of j is 6
Postfix Decrement
The value of x before postfix decrement is 3
The value of x after postfix decrement is 2
The value of y is 3
Java Arithmetic Operator
Binary arithmetic operators include addition, subtraction, multiplication, division, and remainder.
Name
Operator
Description
Addition
+
Adds the operands.
subtraction
-
Subtracts the right operand from the left.
Multiplication
*
Multiplies the operands.
Division
/
Divides the left operand by the right.
Modulo (or) Modulus
%
Computes the remainder.
Example: #3 - Arithmetic operator
Apply each arithmetic operator to a and b.
Java source code
class Operators {
public static void main(String[] args) {
int x = 5, y = 3;
System.out.println("The value of (x + y) is " + (x + y));
System.out.println("The value of (x - y) is " + (x - y));
System.out.println("The value of (x * y) is " + (x * y));
System.out.println("The value of (x / y) is " + (x / y));
System.out.println("The value of (x % y) is " + (x % y));
}
}
Output
The value of (x + y) is 8
The value of (x - y) is 2
The value of (x * y) is 15
The value of (x / y) is 1
The value of (x % y) is 2
Example: #4 - Arithmetic operator division
Compare integer division, which truncates toward zero, with floating-point division.
Java source code
class Operators {
public static void main(String[] args) {
int i = 5, j = 2;
double k = 2.0;
// By default, the data type of the result is int (i.e. 2)
System.out.println("The value of (i / j) is " + (i / j));
// By default, the data type of the result is double (i.e. 2.5)
System.out.println("The value of (i / k) is " + (i / k));
// Cast the divident of type double (i.e. j)
// So the result is 2.5
System.out.println("The value of (i / (double)j) is " + (i / (double)j));
}
}
Output
The value of (i / j) is 2
The value of (i / k) is 2.5
The value of (i / (double)j) is 2.5
Java Relational Operator
Relational operators compare numeric values. Equality operators compare primitive values or check whether two references identify the same object.
Name
Operator
Description
Equal to
==
Compares two values and returns true only if both are equal.
Not Equal to
!=
Compares two values and returns true only if both are not equal.
Less than
<
Compares two values and returns true only if the left operand is less than the right operand.
Greater than
>
Compares two values and returns true only if the left operand is greater than the right operand.
Less than or equal to
<=
Compares two values and returns true only if the left operand is less than or equal to the right operand.
Greater than or equal to
>=
Compares two values and returns true only if the left operand is greater than or equal to the right operand.
Example: #5 - Relational Operator
Compare the operands for equality and order.
Java source code
class Operators {
public static void main(String[] args) {
String name = "John";
int age = 24;
// String Comparision
boolean result = name == "John"; // Same interned literal in this example; == compares references.
System.out.println("Check the name is John : " + result);
// String Comparision with inbuilt inline String method
result = name.equals("John"); // true
System.out.println("Check the name is John using String.equals : " + result);
// Numeric Comparision
result = age == 24; // true
System.out.println("Check the person age is 24 : " + result);
result = age != 20; // true
System.out.println("Check the person age is not equal to 20 : " + result);
result = age < 18; // false
System.out.println("Check the person is minor (i.e. less than 18) : " + result);
result = age > 58; // false
System.out.println("Check the person is senior citizen (i.e. greater than 58) : " + result);
result = age <= 18; // false
System.out.println("Check the person age is less than or equal to 18 : " + result);
result = age >= 24; // true
System.out.println("Check the person age is greater than or equal to 24 : " + result);
}
}
Output
Check the name is John : true
Check the name is John using String.equals : true
Check the person age is 24 : true
Check the person age is not equal to 20 : true
Check the person is minor (i.e. less than 18) : false
Check the person is senior citizen (i.e. greater than 58) : false
Check the person age is less than or equal to 18 : false
Check the person age is greater than or equal to 24 : true
Java Logical Operator
The short-circuit operators && and || combine boolean expressions. The unary ! operator negates a boolean.
Name
Operator
Description
Logical AND
&&
Compares two expressions and returns true only if both evaluate to true.
Logical OR
||
Compares two expressions and returns true either one evaluate to true.
Logical NOT
!
Negates the logical value.
Example: #6 - Logical Operator
Compare the results of AND, OR, and NOT.
Java source code
class Operators {
public static void main(String[] args) {
int john = 32, peter = 19;
// Logical AND operation
boolean result1 = (john > 18) && (peter > 18);
boolean result2 = (john < 18) && (peter < 18);
System.out.println("Both are above 18 years (Logical AND) : " + result1);
System.out.println("Both are below 18 years (Logical AND) : " + result2);
// Logical OR operation
boolean result3 = (john > 18) || (peter > 18);
boolean result4 = (john > 30) || (peter > 30);
boolean result5 = (john > 50) || (peter > 50);
System.out.println("Anyone is above 18 years (Logical OR) : " + result3);
System.out.println("Anyone is above 30 years (Logical OR) : " + result4);
System.out.println("Anyone is above 50 years (Logical OR) : " + result5);
// Logical NOT operation
boolean result6 = !(john > 18);
System.out.println("Check the john's age is below 18 years : " + result6);
}
}
Output
Both are above 18 years (Logical AND) : true
Both are below 18 years (Logical AND) : false
Anyone is above 18 years (Logical OR) : true
Anyone is above 30 years (Logical OR) : true
Anyone is above 50 years (Logical OR) : false
Check the john's age is below 18 years : false
Advertisements
Java Bitwise Operator
Bitwise operators work on integer bit patterns. Shift operators move bits left or right.
Name
Operator
Description
Left Shift
<<
Converts the numerical representation to the equivalent binary representation and shifts a 0 into the rightmost n position
Signed Right Shift
>>
Converts the numerical representation to the equivalent binary representation and shifts 1 only if the number is negative otherwise 0 into the leftmost n position
Unsigned Right Shift
>>>
Converts the numerical representation to the equivalent binary representation and shifts a 0 into the leftmost n position
Bitwise AND
&
Sets a bit when both input bits are set.
Bitwise OR
|
Sets a bit when either input bit is set.
Bitwise XOR
^
Sets a bit when the corresponding input bits differ.
Bitwise Complement
~
Flips each bit of the operand.
Example: #7 - Bitwise Shift Operator
Compare left shift, signed right shift, and unsigned right shift.
Java source code
class Operators {
public static void main(String[] args) {
int x = -6, y = 6, result = 0;
// Left Shift
// Remove the leftmost two binary digits
// and place the binary b'0' to the rightmost 2 position
// to make it 32 bit in length
// x => 11111111 11111111 11111111 11111010 => -6
// x << 2 => 11111111 11111111 11111111 11101000 => -24
// y => 00000000 00000000 00000000 00000110 => 6
// y << 2 => 00000000 00000000 00000000 00011000 => 24
result = x << 2;
System.out.println("Left Shift (x << 2) is : " + result);
result = y << 2;
System.out.println("Left Shift (y << 2) is : " + result);
// Signed Right Shift - Negative Number
// Remove the rightmost two binary digits
// and place the binary b'1' to the leftmost 2 position
// to make it 32 bit in length
// x => 11111111 11111111 11111111 11111010 => -6
// x >> 2 => 11111111 11111111 11111111 11111110 => -2
// Signed Right Shift - Positive Number
// Remove the rightmost two binary digits
// and place the binary b'0' to the leftmost 2 position
// to make it 32 bit in length
// y => 00000000 00000000 00000000 00000110 => 6
// y >> 2 => 00000000 00000000 00000000 00000001 => 1
result = x >> 2;
System.out.println("Signed Right Shift (x >> 2) is : " + result);
result = y >> 2;
System.out.println("Signed Right Shift (y >> 2) is : " + result);
// Unsigned Right Shift
// Remove the rightmost two binary digits
// and place the binary b'0' to the leftmost 2 position
// to make it 32 bit in length
// x => 11111111 11111111 11111111 11111010 => -6
// x >>> 2 => 00111111 11111111 11111111 11111110 => 1073741822
// y => 00000000 00000000 00000000 00000110 => 6
// y >>> 2 => 00000000 00000000 00000000 00000001 => 1
result = x >>> 2;
System.out.println("Unsigned Right Shift (x >>> 2) is : " + result);
result = y >>> 2;
System.out.println("Unsigned Right Shift (y >>> 2) is : " + result);
}
}
Output
Left Shift (x << 2) is : -24
Left Shift (y << 2) is : 24
Signed Right Shift (x >> 2) is : -2
Signed Right Shift (y >> 2) is : 1
Unsigned Right Shift (x >>> 2) is : 1073741822
Unsigned Right Shift (y >>> 2) is : 1
Example: #8 - Bitwise Operator
Compare bitwise AND, OR, XOR, and complement.
Java source code
class Operators {
public static void main(String[] args) {
int x = 6, y = 3, result = 0;
// Bitwise AND
// x => 00000000 00000000 00000000 00000110 => 6
// y => 00000000 00000000 00000000 00000011 => 3
// x & y => 00000000 00000000 00000000 00000010 => 2
result = x & y;
System.out.println("Bitwise AND (x & y) is : " + result);
// Bitwise OR
// x => 00000000 00000000 00000000 00000110 => 6
// y => 00000000 00000000 00000000 00000011 => 3
// x | y => 00000000 00000000 00000000 00000111 => 7
result = x | y;
System.out.println("Bitwise OR (x | y) is : " + result);
// Bitwise XOR
// x => 00000000 00000000 00000000 00000110 => 6
// y => 00000000 00000000 00000000 00000011 => 3
// x ^ y => 00000000 00000000 00000000 00000101 => 5
result = x ^ y;
System.out.println("Bitwise XOR (x ^ y) is : " + result);
// Bitwise Complement
// x => 00000000 00000000 00000000 00000110 => 6
// ~x => 11111111 11111111 11111111 11111001 => -7
result = ~x;
System.out.println("Bitwise Complement ~x is : " + result);
}
}
Output
Bitwise AND (x & y) is : 2
Bitwise OR (x | y) is : 7
Bitwise XOR (x ^ y) is : 5
Bitwise Complement ~x is : -7
Java Assignment Operator
Assignment stores a value. Compound assignments combine an operation with assignment to the same variable.
Name
Operator
Description
Simple Assignment
=
Assigns a value to a variable.
Increment Assignment
+=
Adds a value and the variable and assigns the result to that variable.
Decrement Assignment
-=
Subtracts a value and the variable and assigns the result to that variable.
Multiplication Assignment
*=
Multiplies a value and the variable and assigns the result to that variable.
Division Assignment
/=
Divides a value and the variable and assigns the result to that variable.
Modulo Assignment / Modulus assignment
%=
Modulo a value and the variable and assigns the result to that variable.
Bitwise Left shift assignment
<<=
Performs a Bitwise Left shift operation between a variable and a value, finally assigns the result to that variable.
Bitwise signed right shift assignment
>>=
Performs a Bitwise Signed right shift operation between a variable and a value, finally assigns the result to that variable.
Bitwise Unsigned Right shift assignment
>>>=
Performs a Bitwise Unsigned Right shift operation between a variable and a value, finally assigns the result to that variable.
Bitwise AND assignment
&=
Performs a Bitwise AND operation between a variable and a value, finally assigns the result to that variable.
Bitwise OR assignment
|=
Performs a Bitwise OR operation between a variable and a value, finally assigns the result to that variable.
Bitwise XOR assignment
^=
Performs a Bitwise XOR operation between a variable and a value, finally assigns the result to that variable.
Example: #9 - Arithmetic Assignment operator
Trace x and y through the arithmetic assignments.
Java source code
class Operators {
public static void main(String[] args) {
int x = 6, y = 1;
y += x; // 6 + 1 = 7
System.out.println("The value of y => (y += x) is " + y);
y = 10;
y -= x; // 10 - 6 = 4
System.out.println("The value of y => (y -= x) is " + y);
y = 2;
y *= x; // 6 * 2 = 12
System.out.println("The value of y => (y *= x) is " + y);
y = 18;
y /= x; // 18 / 6 = 3
System.out.println("The value of y => (y /= x) is " + y);
y = 20;
y %= x; // 20 % 6 = 2
System.out.println("The value of y => (y %= x) is " + y);
}
}
Output
The value of y => (y += x) is 7
The value of y => (y -= x) is 4
The value of y => (y *= x) is 12
The value of y => (y /= x) is 3
The value of y => (y %= x) is 2
Example: #10 - Bitwise Assignment operator
Trace x and y through the bitwise assignments.
Java source code
class Operators {
public static void main(String[] args) {
int x = 2, y = 6;
y <<= x; // 6 << 2 = 24
System.out.println("The value of y => (y <<= x) is " + y);
x = 2;
y = 6;
y >>= x; // 6 >> 2 = 1
System.out.println("The value of y => (y >>= x) is " + y);
x = 2;
y = 6;
y >>>= x; // 6 >>> 2 = 1
System.out.println("The value of y => (y >>>= x) is " + y);
x = 3;
y = 6;
y &= x; // 6 & 3 = 2
System.out.println("The value of y => (y &= x) is " + y);
x = 3;
y = 6;
y |= x; // 6 | 3 = 7
System.out.println("The value of y => (y |= x) is " + y);
x = 3;
y = 6;
y ^= x; // 6 ^ 3 = 5
System.out.println("The value of y => (y ^= x) is " + y);
}
}
Output
The value of y => (y <<= x) is 24
The value of y => (y >>= x) is 1
The value of y => (y >>>= x) is 1
The value of y => (y &= x) is 2
The value of y => (y |= x) is 7
The value of y => (y ^= x) is 5