Operators in Java

Beginners code Computer Skills Java Mobile Development Web Development

What are the Operators?

Programs perform various tasks through the instructions given to them. These commands or instructions contain various symbols that denote specific functions. These symbols are called Operators. Let us learn about Operators in Java in this article!

Operators are specific symbols that specify various mathematical or logical instructions used to manipulate the data in a program.

To learn about the operators from video tutorials, refer to this video:

Types of Operators in Java:

Java supports many operators just like C and C++. Operators are divided into 3 categories depending upon the number of operands they work on.

    • Unary Operator
      •  It operates on only 1 operand
      • For example: ++, –, !, ~
    • Binary Operators
      •  It operates on 2 operands
      • For example: &&, %, /, ||
    • Conditional/Ternary Operator
      • It operates on 3 operands

These unary and binary operators perform different functions. And based on these functions, the operators can be further classified into 6 categories. They are:

Arithmetic Operators:

  • Performs manipulations like simple mathematical addition, subtraction, multiplication
  • Assume A and B are 2 integer variables holding values:
    • A = 10
    • B = 5
OperatorFunctionExample
+
(Addition Operator)
Adds the two operandsA + B --> 15
-
(Subtraction Operator)
Subtracts second operand from the first operandA – B --> 5
*
(Multiplication Operator)
Multiplies both the operandsA * B --> 50
/
(Division Operator)
Divides numerator by denominatorA / B --> 2
%
(Modulus Operator)
Gives the remainder after an integer divisionA % B --> 0
++
(Increment operator)
increases integer value by oneA++ --> 11
--
(Decrement operator)
decreases integer value by oneB-- --> 4

Below is a simple program to display arithmetic operators and their functions.

package mypackage;

public class ArithmaticOperators {

  static int A = 10;
  static int B = 5;
  
  public static void main(String[] args) {
    
    System.out.println("Arithmatic Operators !! \n");
    //  "\n" creates an empty line
    
    System.out.println("Addition Operator : " +"A + B = " + (A + B));
    System.out.println("Subtraction Operator : " +"A - B = " + (A - B));
    System.out.println("Subtraction Operator : " +"B - A = " + (B - A));
    System.out.println("Multiplication Operator : " +"A * B = " + (A * B));
    System.out.println("Division Operator : " +"A / B = " + (A / B));
    System.out.println("Modulus Operator : " +"A % B = " + (A % B));

    A++ ; //A is incremented to 11
    System.out.println("Value of A after increment : "+ A);
    
    B--;
    System.out.println("Value of B after increment : "+ B);
    
  }
}

Output:

arithmetic operators

Relational Operators:

  • These are used to comparing two data values or two quantities.
  • They are also called as Comparison operators.
  • It returns a boolean value based upon the condition. If the condition is satisfied, the result is true, else the result is false.
  • Assume A and B are 2 integer variables holding values as
    • A = 10
    • B = 5
OperatorFunctionExample
==
(Is equal to)
Checks if the values of two operands are equal or not. If yes, then the condition becomes true(A == B)
is false
!=
(Is not equal to)
Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true(A != B)
is true
>
(Greater than)
Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true(A > B)
is false
<
(Less than)
Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true(A < B)
is true
>=
(Greater than or equal to)
Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true(A >= B)
is false
<=
(Less than or equal to)
Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true(A <= B)
is true

The program shows the use of relational operators.

package mypackage;

public class RelationalOperators {
  
  static int A = 10;
  static int B = 5;
  
  public static void main(String[] args) {
    
    System.out.println("Relational Operators !! \n"); 
    //  "\n" creates an empty line
    System.out.println("Is equal to Operator : " +"A == B --> " + (A == B));
    System.out.println("Is not equal to Operator : " +"A != B --> " + (A != B));
    System.out.println("Greater than Operator : " +"A > B --> " + (A > B));
    System.out.println("Less than Operator : " +"A < B --> " + (A * B));
    System.out.println("Greater than or equal to Operator : " +"A >= B --> " + (A >= B));
    System.out.println("Less than or equal to Operator : " +"A <= B --> " + (A <= B));
  }
}

 

Output:

Relational operators

Logical Operators:

  • These help to checks multiple conditions.
  • The result depends upon the validity of the individual conditions as well as the validity of the combined statement.
  • They return a boolean value i.e. true or false.
  • Assume A and B are 2 integer variables with values
    • A = 10
    • B = 5.

Let us define 3 conditions:

  • Con1:
    • (A > B) is true.
  • Con2:
    • (A == B) is false.
  • Con3:
    • (A / B == 2) is true.
OperatorFunctionExample
&&
(Logical AND operator)
If both the operands are non-zero or true, then condition becomes true. Con1 && Con2
gives false
||
(Logical OR Operator)
If any of the two operands is nonzero or true, then condition becomes true. Con1 || Con2
gives true
!
(Logical NOT Operator)
It reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false and vice-versa.

!Con1
gives false

The below program displays the use of logical operators.

package mypackage;

public class LogicalOperators {

  static int A = 10;
  static int B = 5;
  
  public static void main(String[] args) {
    
    Boolean Condition1 = (A > B);
    Boolean Condition2 = (A == B);
    Boolean Condition3 = (A / B == 2);
    
    System.out.println("Condition 1 is "+ Condition1);
    System.out.println("Condition 2 is "+ Condition2);
    System.out.println("Condition 3 is "+ Condition3+"\n");
    
    System.out.println("Logical Operators !! \n"); 
    //  "\n" creates an empty line
    System.out.println("Logical AND Operator : (Condition1 && Condition2) " + (Condition1 && Condition2));
    System.out.println("Logical AND Operator : (Condition1 && Condition3) " + (Condition1 && Condition3));
    System.out.println("Logical OR Operator : (Condition1 || Condition2) " + (Condition1 || Condition2));
    System.out.println("Logical NOT Operator (Condition 1) : " + (!Condition1));
    System.out.println("Logical NOT Operator (Condition 2 : " + (!Condition2));
    
  }
}

Output

Logical Operators

Bitwise Operators:

  • It performs operations on individual bits of a number.
  • It works bit-by-bit to give the result.
  • The truth tables for & (AND), | (OR), and ^ (XOR) are as follows:
pqp & qp | qp ^ q
00000
01011
10011
11110
  • Assume if
    • A = 7;
    • B = 3;
  • In binary format
    • A = 0111
    • B = 0011
  • Bitwise AND
    •  It returns bit by bit AND operation of the operands
    • A&B = 0011, In decimal A&B = 3
  • Bitwise OR
    •  This operator performs OR operation bit by bit on the operands.
    • A|B = 0111, In decimal A|B = 7
  • Bitwise XOR
    •  Returns bit by bit XOR operation of the operands.
    • If both the bits are same, gives 0 else gives 1.
    • A^B = 0100, In decimal A^B = 4
  • Bitwise NOT
    •  Inverses each bit of the input operand.
    • It converts each 0 to 1 and each 1 to 0.
    • ~A  = 1000, In decimal ~A = 8
  • Binary Left Shift Operator
    •  The left operand’s bits are moved left by the number of bits specified by the right operand.
    • It multiplies the operand by 2^n.
      • The power n is equal to the number of bits specified by the right operand.
    • A = 0111, In decimal A= 7
    • A << 1 = 1110, In decimal A << 1 = 14
  • Binary Right Shift Operator
    • The left operand’s bits are moved right by the number of bits specified by the right operand.
    • It divides the operand by 2^n.
      • The power n is equal to the number of bits specified by the right operand.
    • A = 0111, In decimal A= 7
    • A >> 1 = 0011, In decimal A >> 1 = 3

Assignment Operators

  • Assignment operators assign the result of an expression to a variable.
  • It is a binary operator.
  • The value of the right operand is assigned to the left operand.
OperatorFunctionExample
=
(Assign)
Assigns values from right side operands to left side operandK = 3
A = B
+=
(Increments, then assigns)
It adds the right operand to the left operand and assigns the result to the left operandC += A means
C = C+A
-=
(Decrements, then assigns)
It subtracts the right operand from the left operand and assigns the result to the left operandC -= A means
C = C-A
*=
(Multiplies, then assigns)
It multiplies the right operand with the left operand and assigns the result to the left operandC *= A means
C = C*A
/=
(Divides, then assigns)
It divides the left operand with the right operand and assigns the result to the left operandC /= A means
C = C/A
%=
(Modulus, then assigns)
It takes modulus using two operands and assigns the result to the left operandC %= A means
C = C%A

Ternary Operator (Conditional Operator):

  • It works on 3 operands
  • It consists of 2 parts:
    • Condition ? Expression1 : Expression2
    • If Condition is true then it returns value of Expression1 otherwise returns the value of Expression2

The program below displays the use of the conditional operator:

package mypackage;

public class TernaryOperator {
  
  static int A = 10;
  static int B = 6;
  
  public static void main(String[] args) {
    
    int result1;
    int result2;
    
    result1 = (A / B == 2) ? (A - B) : (A + B);
    System.out.println("Result 1 is : "+result1);
      
    result2 = (A / B != 2) ? (A - B) : (A + B);
    System.out.println("Result 2 is : "+result2);	
  }
}

Output:

ternary operator

Special Operators:

Java also supports some special type of operators like:

  • Dot Operator
  • InstanceOf Operator

Dot Operator (.):

  • Dot operator helps to access:
    • variable/constant
    • method
    • Class
    • package
    • For Example: java.lang.System.out.println("Hello");

dot operator in java

The instanceof Operator:

  • This operator checks the existence of an object and returns a boolean value.
  • It is also called a type/object comparison operator.
    • it compares if the object belongs to the specified type.
  • Applying the instanceof operator with any object having a null value, returns false.
  • It can also identify objects of derived and inherited classes.

The below program explains the use of the instanceof operator.

package mypackage;

public class CheckInstance {

  public static void main(String[] args) {
    
    CheckInstance ob = new CheckInstance();
    
    CheckInstance object = null;
    
    System.out.println("ob instanceof CheckInstance :"+(ob instanceof CheckInstance));
    
    System.out.println("object instanceof CheckInstance"+(object instanceof CheckInstance));
    
  }	
}

Output:

instancof

 

In the next article, I will discuss looping control in Java. The article will cover different loops available in Java and how to use them.