JAVA OPERATORS

Operators in Java are one of the most important topics in Java Programming Language. They are used to perform various types of operations on Variables and their values.

Table of Contents

[hide]
  • Types of Java Operators
  • Arithmetic Operator in Java
  • Relational Operator in Java
  • Logical Operator in Java
  • Ternary Operator in Java
  • Assignment Operator in Java
  • Bitwise Operator in Java
  • Shift Operator in Java
  • Unary Operator in Java

Types of Java Operators

We all have been learning the basic Operators since our childhood, in school days we all have used the four basic mathematical operators i.e addition, substraction, multiplication and division. Java Operators are also used for the same purpose for performing certain operations that give us the desired output. There are eight types of operators in Java :

  • 1. Arithmetic
  • 2. Relational
  • 3. Logical
  • 4. Ternary
  • 5. Assignment
  • 6. Bitwise
  • 7. Shift
  • 8. Unary
java-operators

Arithmetic Operator in Java

These operators are used in performing mathematical operations on the values. It accepts numeric values as operands and perform Operations on them giving the desired single numeric value.

OperatorShort DescriptionExamples
Addition ( + )Add the values of two operandsX + Y = 20
Subtraction ( - )Substract the values of two operandsX - Y = 10
Multiplication ( * )Multiply the values of two operandsX * Y = 40
Division ( / )Multiply the values of two operandsX / Y = 20
Modulus ( % )Divide the values of two operands and returns remainderX % Y = 2

Example

copyimage
// Java program to illustrate Arithmetic operators in Java
public class JavaOperatorExample{
    public static void main(String args[]){  
          int X = 10; 
          int Y = 30; 
          System.out.println("Addition of X and Y is : " + (X + Y));
          System.out.println("Substraction of X and Y is : " + (X - Y));
          System.out.println("Multiplication of X and Y is : " + (X * Y));
          System.out.println("Division of X and Y is : " + (X / Y));
          System.out.println("Modulus of X and Y is : " + (X % Y));
     }
}

OUTPUT:

Addition of X and Y is : 40
Substraction of X and Y is : -20
Multiplication of X and Y is : 300
Division of X and Y is : 0
Modulus of X and Y is : 10

Relational Operator in Java

Relational operator in java is used to compare the equality of the operands or the values. These are mainly used in Java Control Statements which we will learn about in later modules.

Let us assume the value of X be 20 and Y be 30 respectively

OperatorShort DescriptionExamples
Equal to ( == )if values of either side of the operands are equal the condition becomes true.(X == Y) is false
Not Equal to ( != )if values of either side of the operands are not equal the condition becomes true.(X != Y) is true
Greater than ( > )if value of left operand is greater than the value of right the condition becomes true.(X > Y) is false
Less than ( < )if value of left operand is less than the value of right the condition becomes true.(X < Y) is true
Greater than or equal to ( >= )if value of left operand is greater than or equal to the value of right the condition becomes true.(X >= Y) is false
Less than or equal to ( <= )if value of left operand is lessthan or equal to the value of right the condition becomes true.(X <= Y) is true

Example

copyimage
// Java program to illustrate Relational operators in Java
public class JavaOperatorExample{
    public static void main(String args[]){  
          int X = 10; 
          int Y = 30;
          System.out.println("X is equal to Y : " + (X == Y));
          System.out.println("X is not equal to Y : " + (X != Y));
          System.out.println("X is greater than Y : " + (X > Y));
          System.out.println("X is less than Y : " + (X < Y));
          System.out.println("X is greater than equal to Y : " + (X >= Y));
          System.out.println("X is less than equal to Y : " + (X <= Y));
     }
}

OUTPUT:

X is equal to Y : false
X is not equal to Y : true
X is greater than Y : false
X is less than Y : true
X is greater than equal to Y : false
X is less than equal to Y : true

Logical Operator in Java

Logical Operators are often used with loops and conditional statements to determine the logic between operands or values. These operators are similar to the AND gate and OR gate in digital electronic. If the first condition is false, it does not evaluate the second consition.

Let us assume the value of X is 10

OperatorShort DescriptionExamples
Logical AND ( && )If both conditions are true on either side,it returns truex<20&&x<30
Logical OR ( || )If any condition is true on either side,it returns truex<20||x<5
Logical NOT ( ! )returns true if the operand produces false result! (x<10&&x<5)

Example

copyimage
// Java program to illustrate Logical operators in Java
public class JavaOperatorExample{
    public static void main(String args[]){  
          int X = 10; 
          int Y = 10;
          System.out.println("X && Y  :" + ( X<7 && Y>5));
          System.out.println("X || Y  :" + ( X<5 || Y>5));
          System.out.println("!(X && Y ) :" + !( X<5 && Y>5));
     }
}

OUTPUT:

X && Y :false
X || Y :true
!(X && Y ) :true

Ternary Operator in Java

It is also known as conditional operator, it works with three operands . We can use ternary operator as an alternative to if else statements which we will learn in later modules.

Syntax

variable result = (expression) ? (Statement1) :(Statement2)

If the output of expression is true, then first statement will be executed before the colon (:) else if expression evaluates to false then second statement will be executed. Let us see this with example.

Example

copyimage
// Java program to illustrate ternary operator in Java
public class TernaryExample{
     public static void main(String args[]){  
          int value =20;
          int result = (value == 20) ? value : 40; // Statement1 will get executed
          int resultsecond = (value == 25) ? value : 40; // Statement2 will get executed
          System.out.println("result is :" + result);
          System.out.println("resultsecond is :" + resultsecond);
     }
}

OUTPUT:

result is :20
resultsecond is :40

Assignment Operator in Java

To assign values to a variable assignment operator is used, the right side operand value is assigned to the left side operand. They are mostly used with Arithmetic Operators. The result of Arithmetic operation is assigned to the variable with the help of assignment operator.

OperatorShort DescriptionExamplesAlso Used As
+=Used to add the operand on both the sides and thus assigning result to the left operandx += yx = x + y
+=Used to subtract the right operand with left and thus assigning result to the left operandx -= yx = x - y
*=Used to multiply operand on both the sides and thus assigning result to the left operandx *= yx = x * y
/=Used to divide left operand with right and thus assigning result to the left operandx /= yx = x / y
%=Used to calculate modulus on both sides and thus assigning result to the left operandx %= yx = x % y
^=Used to raise left operand value to the power of right operator.x ^= yx = x ^ y

Example

copyimage
// Java program to illustrate Assignment operators in Java
public class JavaOperatorExample{
    public static void main(String args[]){  
         int X = 10; 
         int Y = 20; 
         System.out.println("Y += X is : " + (Y += X));
         System.out.println("Y -= X is : " + (Y -= X));
         System.out.println("Y *= X is : " + (Y *= X));
         System.out.println("Y /= X is : " + (Y /= X));
         System.out.println("Y %= X is : " + (Y %= X));
         System.out.println("Y ^= X is : " + (Y ^= X));
     }
}

OUTPUT:

Y += X is : 30
Y -= X is : 20
Y *= X is : 200
Y /= X is : 20
Y %= X is : 0
Y ^= X is : 10

Bitwise Operator in Java

Bitwise operator in java is used for performing bitwise manipulation i.e bit by bit. It converts the numeric values into the binary format and then performs the operations and get the desired output.

OperatorShort DescriptionExamples
Bitwise AND ( & )performs bit by bit AND operationa & b
Bitwise OR ( | )performs bit by bit OR operationa | b
Bitwise XOR ( ^ )performs bit by bit XOR operationa ^ b
Bitwise Compliment ( ~ )returns the reversed bits of the operation ~a

Truth table for bitwise operators is as follows :

aba & ba | ba ^ b
00000
01011
10011
11110

Example:

25 = 1 1 0 0 1

15 = 0 1 1 1 1

Now if we perform bitwise operations on them, the result would be as follows :

AND Bitwise Operator

1 1 0 0 1

1 1 0 0 1 &

0 1 0 0 1 = 9 in decimal

OR Bitwise Operator

1 1 0 0 1

1 1 0 0 1 |

1 1 1 1 1 = 31 in decimal

XOR Bitwise Operator

1 1 0 0 1

1 1 0 0 1 ^

1 0 1 1 0 = 22 in decimal

Example

copyimage
// Java program to illustrate Bitwise operators in Java
public class JavaOperatorExample{
    public static void main(String args[]){  
         int X = 25; 
         int Y = 15;
         System.out.println("X & Y is : " + (X & Y));
         System.out.println("X | Y is : " + (X | Y));
         System.out.println("X ^ Y is : " + (X ^ Y));
     }
}

OUTPUT:

X & Y is : 9
X | Y is : 31
X ^ Y is : 22

Shift Operator in Java

Java Shift Operator is used to manipulate bits by shifting it to the respective side of the operands as specified, i.e. the left-shift operator or the right-shift operator.

OperatorShort DescriptionExamples
left shift ( << )Add zeros according to the shift value,to the right side of the bitsa << b
right shift ( << )shift the bits of the number to the right according to the shit value.a >> b

Example

copyimage
// Java program to illustrate Shift operators in Java
public class JavaOperatorExample{
    public static void main(String args[]){  
         int X = 8;  // 1 0 0 0
         int Y = X << 2;  // 1 0 0 0 0 0
         int Z = X >> 2;  // 1 0
         System.out.println("value of left shift is : " + Y);
         System.out.println("value of right shift is : " + Z);
     }
}

OUTPUT:

value of left shift is : 32
value of right shift is : 2

Unary Operator in Java

Unary operator in java requires one argument and is used to increment/decrement/negate or invert a value.

OperatorShort DescriptionExamples
increment ( ++ )use to increment the given value by 1x ++ and ++ x
decrement ( - - )use to decrement the given value by 1x -- and -- x
decrement ( ! )use to invert the boolean value! x

Example

copyimage
// Java program to illustrate Unary operators in Java
public class JavaOperatorExample{
    public static void main(String args[]){  
         int X = 20; 
         boolean Y = true; 
         System.out.println("value of X++ is : " + X++); //20 -> 21
         System.out.println("value of ++X is : " + ++X); //22
         System.out.println("value of X-- is : " + X--); //22 -> 21
         System.out.println("value of --X is : " + --X); //20
         System.out.println("value of !Y is : " + !Y); //false
     }
}

OUTPUT:

value of X++ is : 20
value of ++X is : 22
value of X-- is : 22
value of --X is : 20
value of !Y is : false