Operators in JAVA

In this chapter, we will discuss all the operators in Java. The operator is a symbol that is used to perform operations.  It represents an action. For example: +, -, *, / etc. There are different types of Operators in Java.

Types of Operator in Java

1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. instance of operator
9. Precedence and Associativity

1. Arithmetic Operators in java

Arithmetic operators are used to performing arithmetic operations on primitive data types. JAVA have some arithmetic operators:

Operators Symbol
Multiple *
Divide /
Modulo %
Addition +
Subtraction
public class ArithmeticOperators 
{ 
   public static void main(String[] args) 
   { 
      int a = 10, b = 5;
      String x = "JAVA", y = "Program"; 

      // Example of * operator
      System.out.println("a * b = " + (a * b)); 
	
      // Example of / operator
      System.out.println("a / b = " + (a / b)); 
		
      // Example of + operator with numeric data
      System.out.println("a + b = " + (a + b)); 
		
      // Example of + operator with String data and concatenate the string
      System.out.println("x + y = " + (x + y)); 
		
      // Example of - operator 
      System.out.println("a - b = " + (a - b)); 
		
      // Example of % operator. It  gives remainder on dividing first operand with second  
      System.out.println("a % b = " + (a % b)); 
    } 
} 

Output:
a * b = 50
a / b = 2
a + b = 15
x + y = JAVAProgram
a – b = 5
a % b = 0

2. Unary Operators in java

Unary operators required only one operator. Unary operators are used to performing various operations:

  • Unary minus (-): It used to convert in a negative number
  • Unary plus (+): It used to convert in a positive number
  • Increment operator (++): It increments the value by 1. There are two variations of increment operators.
    • Post-Increment: First use the variable after that increment the value of a variable.
    • Pre-Increment: It is the opposite of post-increment. In this first increment, the value and after that used the value of a variable.
  • Decrement operator (–): It decrements the value by 1. There are two variations of the decrement operator.
    • Post-Decrement: First use the variable after that decrement the value of a variable.
    • Pre- Decrement: It is the opposite of post decrement. In this first decrements, the value and after that used the value of a variable.
  • Logical not operator (!): It is used with a boolean value. It inverts the value of boolean.

Example of Increment and decrement operators:      

class UnaryOperatorExample
{  
   public static void main(String args[])
   {  
	int a = 5;  
	// It will print 5 because in post-increment value used first then increment
	System.out.println(a++);
	// After increment value of a is 6.
	
	// It will print 7 because in pre-increment value increment first then used
	System.out.println(++a);
	// After increment value of a is 7.
	
	// It will print 7 because in post-decrement value used first then decrement
	System.out.println(a--);
	// After decrement value of a is 6.
	
	// It will print 5 because in pre-decrement value decrement  first then used
	System.out.println(--a);
	// After decrement value of a is 5.
   }
}  

Output:
5
7
7
5

Example of Logical not operators:

class UnaryOperatorExample
{  
   public static void main(String args[])
   {  
      boolean a = false;  
      System.out.println("Value of a by use of logical and operator: "+ !a);
   }
}  

Output: Value of a by use of logical and operator: true

3. Assignment Operator

  • The assignment operator is used to assign value to a variable.
  • The value given on the right-hand side of the operator is assigned to the variable on the left.
  • The right side value must be constant or declared before used.
variable_Name = value;

Shorthand operator: When an assignment operator is combined with another operator. Like +=, *= etc.

If the assignment operator combined with other operators to build a shorter version of a statement called Compound Statement

Shorthand operator Description
*= Multiplying left operand with right operand and then assigning it to the variable on the left.
/= Dividing left operand with right operand and then assigning it to the variable on the left.
%= Assigning modulo of left operand with right operand and then assigning it to the variable on the left.
+= Adding left operand with right operand and then assigning it to the variable on the left.
-= Subtracting left operand with right operand and then assigning it to the variable on the left.

Example of Shorthand operators:

class UnaryOperatorExample
{
   public static void main(String args[])
   {
      int a = 1, b = 2, c = 3, d = 4;
      a += 1;
      b -= 1;
      c *= 3;
      d /= 2;
      System.out.println("Value of a="+ a);
      System.out.println("Value of b="+ b);
      System.out.println("Value of c="+ c);
      System.out.println("Value of d="+ d);
}

Value of a=2
Value of b=1
Value of c=9
Value of d=2

4. Relational operator

These operators are used to check the relations between two values. It returns the boolean result after the comparison. The relational operator is mostly used in looping and conditional statements. Some Relational operators:

  • Equals to (= =): It returns true if the left-hand side and right-hand side are equal otherwise false.
  • Not equals (! =): It returns true if the left-hand side does not equal to right-hand side otherwise true.
  • Less than (<): It returns true if the left-hand side is less than from the right-hand side otherwise false.
  • Less than equals to (<=): It returns true if the left-hand side is less than or equals to the right-hand side otherwise false.
  • Greater than (>): It returns true of the left-hand side is greater than the right-hand side.
  • Greater than equals to(>=): It returns true of the left-hand side is greater than or equals to the right-hand side.

Example of Relational Operators:     

class RelationalOperatorExample
{  
   public static void main(String args[])
   {  
	int a = 1, b = 1, c = 3, d = 4;
	System.out.println("Is a == b ="+ (a == b));
	System.out.println("Is a != c ="+ (a != c));
	System.out.println("Is b < c ="+ (b< c));
	System.out.println("Is a <= b ="+ (a <= b));
	System.out.println("Is c > b ="+ (c > b));
	System.out.println("Is a >= c ="+ (a >= c));
   }  
}

Is a == b =true
Is a != c =true
Is b < c =true
Is a <= b =true
Is c > b =true
Is a >= c =false

5. Logical Operators

These operators are used to perform “logical AND” and “logical OR” operation. Working logical operators are the same as Logical gates in digital. Logical operators three types are:

  • Logical AND (&&): If will return true if both conditions are true
  • Logical OR (||): It will return true if at least one condition is true
  • Logical NOT(!): It is used to reverse the state of the variable

Example of Logical Operators:

class LogicalOperatorExample
{
   public static void main(String args[])
   {
      int a = 0;
      int b = 1;
      boolean c = false;

      // In this case both condition are true . So it will print the statement
      if((a < 1) && (b < 2))
      {
         System.out.println("AND operation is executed");
      }

      // In this case first condition(a < 0) is false.
      // So it will not any print the statement
      if((a < 0) && (b < 2))
      {
         System.out.println("AND operation is executed");
      }

      // In this case first condition is true and second is false.
      //But OR operator need at least one condition true.
      // So it will print the statement
      if((a < 1) || (b < 0))
      {
         System.out.println("OR operation is executed");
      }

      // In this case first condition is false and second is true. 
      // But OR operator need at least one condition true.
      // So it will print the statement
      if((a < 0) || (b < 2))
      {
         System.out.println("OR operation is executed");
      }

      // NOT perform reverse operation. So it will return value of c is true
      if(!c)
      {
         System.out.println("NOT operation is executed");
      }
   }
}

OR operation is executed
OR operation is executed
OR operation is executed
NOT operation is executed

6. Ternary operator

The ternary operator is an alternate version of the if-else statement. It is based on condition. It has three operands.

Condition ? first Statement : second Statement;

If the condition is true, then the first statement will be executed another second statement.

class TernaryOperatorExample
{  
   public static void main(String args[])
   {  
	int a = 5;
	int b = 2;
	int result = 0;
	// Here we try to find bigger number.
        //If "a" is greater than "b" then "a" will assign to result otherwise "b";
	result = (a > b) ? a : b;
	System.out.println("The bigger number is : "+  result);		
    }  
}

Output: The bigger number is : 5

7. Bitwise Operators 

JAVA has some Bitwise operator that can be applied on any integer types, long, int, short, char, and byte. Bitwise operators work on bits of a number.

  • Bitwise AND operator (&): It will convert the operands to the binary digit and copies a bit to the result if it exists in both operands.
  • Bitwise OR operator ( | ): It will convert the operands to the binary digit and copies a bit if it exists in either operand.
  • Bitwise XOR operator (^): It will convert the operands to the binary digit and copies the bit if it is set in one operand but not both.
  • Bitwise Complement Operator (~): This is a unary operator that returns the one’s complement representation of the input value, i.e. with all bits inverted.

How Bitwise Operator is working?

Let’s take two operands
int a = 5;
int b = 3

Bitwise operator works on bits. So firstly, the compiler converts both the operand in binary form.
a = 101
b = 011

Bitwise AND operator (&): It will copy the bit which is 1 in both operands.
1   0   1
0   1   1 
Result of a&b = 0   0  1

Bitwise OR operator ( | ): It will copy the bit 1 either in the first operand or in second operands.
1   0   1
0   1   1 
Result of a|b = 1   1  1

Bitwise XOR operator ( ^ ): It will copy the bit 1 if it is set in one operand but not both.
1   0   1
0   1   1 
Result of a^b = 1   1  0

Bitwise complement operator ( ~ ): It returns the one’s complement representation of the input value
1   0   1
Result of  ~a = 0   1  0

8. instance of an operator

It is used for type check the object. It can be used to test an object is an instance of a class or an interface. It can apply only non-primitive data types.

Object instanceOf className/InterfaceName;
class InstanceOfOperatorExample
{
   public static void main(String args[])
   {
      InstanceOfOperatorExample a = new InstanceOfOperatorExample();
      System.out.println(a instanceof InstanceOfOperatorExample);
   }
}

Output: true

9. Precedence and Associativity of Operators in Java

As we already discuss all the Operator in Java. So now we are starting with the Precedence and associative are rules. There are certain rules followed by all Operators in Java. That would be used when we are solving any equations which have more than one type of operator. In such type of cases, these rules determine which part of the equation will be evaluated first. Here we have a table that represents the precedence of operators from high to low.

Operators in JAVA

1. Quiz, Read the below code and do answer.

public class JavaOperators
{ 
     public static void main(String args[])
     {
    	 int b = 5 - -7;
    	 System.out.println(b);
     }
}

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

public class JavaOperators
{ 
     public static void main(String args[])
     {
    	 short a = 1;
    	 short b = a + 2;
    	 System.out.println(b);
     }
}

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

public class JavaOperators
{ 
     public static void main(String args[])
     {
    	 int c=5, d=6;
    	 if(c++ == --d)
    	 {
    		 System.out.println("Values are equal");
    	 }
    	 else
    	 {
    		 System.out.println("Values are not equal");
    	 }
     }
}

Click on anyone to know the answer.

4. Quiz, Read the below code and do answer.

public class JavaOperators
{ 
     public static void main(String args[])
     {
    	 int a=6, b=5;
    	 if(++b == a--)
    	 {
    		 System.out.println("JavaGoal.com");
    	 }
    	 else
    	 {
    		 System.out.println("JavaGoal");
    	 }
     }
}

Click on anyone to know the answer.

5. Quiz, Read the below code and do answer.

public class JavaOperators
{ 
     public static void main(String args[])
     {
    	 double value = 1 + 5; 
         double value2 = value / 4;
         int value3 = 1 + 5;
         int value4 = value3 / 4;
         System.out.print(value2 + " " + value4);
     }
}

Click on anyone to know the answer.

6. Quiz, Read the below code and do answer.

public class JavaOperators
{ 
     public static void main(String args[])
     {
    	 int a = 1;
         int b = 2;
         int c;
         int d;
         c = ++b;
         d = a++;
         c++;
         b++;
         ++a;
         System.out.println(a + " " + b + " " + c);
     }
}

Click on anyone to know the answer.

7. Quiz, Read the below code and do answer.

public class JavaOperators
{ 
     public static void main(String args[])
     {
    	 int y=18;
    	 String value = (y < 15) ? "s" : (y < 22)? "Java" : "Goal";
    	 System.out.println(value);
     }
}

Click on anyone to know the answer.

Leave a Comment