Java Operators
Java Operators
Operators in Java are the symbols used for performing specific operations in Java. Operators
make tasks like addition, multiplication, etc which look easy although the implementation of
these tasks is quite complex.
1. Arithmetic Operators
They are used to perform simple arithmetic operations on primitive data types.
* : Multiplication
/ : Division
% : Modulo
+ : Addition
– : Subtraction
Example:
// Drive Class
class arithmetic {
// Main Function
public static void main (String[] args) {
// Arithmetic operators
int a = 10;
int b = 3;
}
}
Output
a + b = 13
a-b=7
a * b = 30
a/b=3
a%b=1
2. Unary Operators
Unary operators need only one operand. They are used to increment, decrement, or negate a
value.
– : Unary minus, used for negating the values.
+ : Unary plus indicates the positive value (numbers are positive without this, however). It
performs an automatic conversion to int when the type of its operand is the byte, char, or
short. This is called unary numeric promotion.
++ : Increment operator, used for incrementing the value by 1. There are two varieties of
increment operators.
Post-Increment: Value is first used for computing the result and then
incremented.
Pre-Increment: Value is incremented first, and then the result is computed.
– – : Decrement operator, used for decrementing the value by 1. There are two varieties
of decrement operators.
Post-decrement: Value is first used for computing the result and then
decremented.
Pre-Decrement: The value is decremented first, and then the result is
computed.
! : Logical not operator, used for inverting a boolean value.
Example:
// Driver Class
class unary {
// main function
public static void main(String[] args)
{
// Interger declared
int a = 10;
int b = 10;
Output
Postincrement : 10
Preincrement : 12
Postdecrement : 10
Predecrement : 8
3. Assignment Operator
‘=’ Assignment operator is used to assign a value to any variable. It has right-to-left
associativity, i.e. value given on the right-hand side of the operator is assigned to the variable
on the left, and therefore right-hand side value must be declared before using it or should be a
constant.
The general format of the assignment operator is:
variable = value;
In many cases, the assignment operator can be combined with other operators to build a shorter
version of the statement called a Compound Statement. For example, instead of a = a+5, we
can write a += 5.
+=, for adding the left operand with the right operand and then assigning it to the variable
on the left.
-=, for subtracting the right operand from the left operand and then assigning it to the
variable on the left.
*=, for multiplying the left operand with the right operand and then assigning it to the
variable on the left.
/=, for dividing the left operand by the right operand and then assigning it to the variable on
the left.
%=, for assigning the modulo of the left operand by the right operand and then assigning it
to the variable on the left.
Example:
// Java Program to implement
// Assignment Operators
import java.io.*;
// Driver Class
class operation {
// Main Function
public static void main(String[] args)
{
// Assignment operators
int f = 7;
System.out.println("f += 3: " + (f += 3));
System.out.println("f -= 2: " + (f -= 2));
System.out.println("f *= 4: " + (f *= 4));
System.out.println("f /= 3: " + (f /= 3));
System.out.println("f %= 2: " + (f %= 2));
System.out.println("f &= 0b1010: " + (f &= 0b1010));
System.out.println("f |= 0b1100: " + (f |= 0b1100));
System.out.println("f ^= 0b1010: " + (f ^= 0b1010));
System.out.println("f <<= 2: " + (f <<= 2));
System.out.println("f >>= 1: " + (f >>= 1));
System.out.println("f >>>= 1: " + (f >>>= 1));
}
}
Output
f += 3: 10
f -= 2: 8
f *= 4: 32
f /= 3: 10
f %= 2: 0
f &= 0b1010: 0
f |= 0b1100: 12
f ^= 0b1010: 6
f <<= 2: 24
f >>= 1: 12
f >>>= 1: 6
4. Relational Operators
These operators are used to check for relations like equality, greater than, and less than. They
return boolean results after the comparison and are extensively used in looping statements as
well as conditional if-else statements. The general format is,
variable relation_operator value
// Driver Class
class myclass {
// main function
public static void main(String[] args)
{
// Comparison operators
int a = 10;
int b = 3;
int c = 5;
Output
a > b: true
a < b: false
a >= b: true
a <= b: false
a == c: false
a != c: true
5. Logical Operators
These operators are used to perform “logical AND” and “logical OR” operations, i.e., a
function similar to AND gate and OR gate in digital electronics. One thing to keep in mind is
the second condition is not evaluated if the first one is false, i.e., it has a short-circuiting effect.
Used extensively to test for several conditions for making a decision. Java also has “Logical
NOT”, which returns true when the condition is false and vice-versa
Conditional operators are:
&&, Logical AND: returns true when both conditions are true.
||, Logical OR: returns true if at least one condition is true.
!, Logical NOT: returns true when a condition is false and vice-versa
Example:
// Driver Class
class myclass {
// Main Function
public static void main (String[] args) {
// Logical operators
boolean x = true;
boolean y = false;
Output
x && y: false
x || y: true
!x: false
6. Ternary operator
The ternary operator is a shorthand version of the if-else statement. It has three operands and
hence the name Ternary.
The general format is:
condition ? if true : if false
The above statement means that if the condition evaluates to true, then execute the statements
after the ‘?’ else execute the statements after the ‘:’.
Example:
// Java program to illustrate
// max of three numbers using
// ternary operator.
public class operators {
public static void main(String[] args)
{
int a = 20, b = 10, c = 30, result;
Output
Max of three numbers = 30
7. Bitwise Operators
These operators are used to perform the manipulation of individual bits of a number. They can
be used with any of the integer types. They are used when performing update and query
operations of the Binary indexed trees.
&, Bitwise AND operator: returns bit by bit AND of input values.
|, Bitwise OR operator: returns bit by bit OR of input values.
^, Bitwise XOR operator: returns bit-by-bit XOR of input values.
~, Bitwise Complement Operator: This is a unary operator which returns the one’s
complement representation of the input value, i.e., with all bits inverted.
// Driver class
class myclass {
// main function
public static void main(String[] args)
{
// Bitwise operators
int d = 0b1010;
int e = 0b1100;
System.out.println("d & e: " + (d & e));
System.out.println("d | e: " + (d | e));
System.out.println("d ^ e: " + (d ^ e));
System.out.println("~d: " + (~d));
System.out.println("d << 2: " + (d << 2));
System.out.println("e >> 1: " + (e >> 1));
System.out.println("e >>> 1: " + (e >>> 1));
}
}
Output
d & e: 8
d | e: 14
d ^ e: 6
~d: -11
d << 2: 40
e >> 1: 6
e >>> 1: 6
8. Shift Operators
These operators are used to shift the bits of a number left or right, thereby multiplying or
dividing the number by two, respectively. They can be used when we have to multiply or
divide a number by two. General format-
number shift_op number_of_places_to_shift;
<<, Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a
result. Similar effect as multiplying the number with some power of two.
>>, Signed Right shift operator: shifts the bits of the number to the right and fills 0 on
voids left as a result. The leftmost bit depends on the sign of the initial number. Similar
effect to dividing the number with some power of two.
>>>, Unsigned Right shift operator: shifts the bits of the number to the right and fills 0
on voids left as a result. The leftmost bit is set to 0.
// Driver Class
class myclass {
// main function
public static void main(String[] args)
{
int a = 10;
Output
a<<1 : 20
a>>1 : 5
9. instanceof operator
The instance of the operator is used for type checking. It can be used to test if an object is an
instance of a class, a subclass, or an interface. General format-
object instance of class/subclass/interface
class operators {
public static void main(String[] args)
{
interface MyInterface {
}
Output
obj1 instanceof Person: true
obj1 instanceof Boy: false
obj1 instanceof MyInterface: false
obj2 instanceof Person: true
obj2 instanceof Boy: true
obj2 instanceof MyInterface: true