A Complete Java Operators Tutorial

Java Operators are symbols that are used to perform some special operations. In this tutorial, you will learn about Java operators and how we can use operators in Java with code examples.

Introduction

 
Java Operators are an integral part of Java programming language. This article will explain Java operators and how java operators work with code example. Java operators are classified and listed according to precedence order. Java operators are generally used to manipulate primitive data types.
 

What are Java Operators?

 
Operators are some special symbols that are used to perform mathematical and logical operations on the operands and returns the result. There are several types of operators in java which are used to perform the different operations. For example, the  (+) operator symbol is used to add two or more operands and returns the addition as a result.
 

Operators in Java 

 
We can divide Java operators into the following 7 major categories:
  1. Assignment Operator
  2. Arithmetic Operators 
  3. Relational Operators 
  4. Unary Operators
  5. Logical Operators
  6. Conditional(Ternary) Operators
  7. InstanceOf Operator
  8. Bitwise Operators 

1) Java Assignment Operator. 

 
An assignment operator is one of the most used and common operators, which we use to assign a value on its right to the operand on its left. For example, the following code sample assigns number 5 to an int a variable and string "Hello" is assigned to string msg variable-
 
int a = 5;
String msg = "Hello";
 
The complete program is listed below.
 
Code Example:
  1. public class AssignmentOperators {    
  2.     public static void main(String args []) {    
  3.     
  4.         int a = 5;    
  5.         String msg= "Hello" ;    
  6.     
  7.         System.out.println(+a);    
  8.         System.out.println(msg+" CSharpCorner");    
  9.     
  10.     }    
  11. }     
The output of the above code generates the following output.
 
art
 

2) Java Arithmetic Operators.

 
Arithmetic operators are used to achieve mathematical calculations like algebra. The following table lists arithmetic operators.
 
 Operators 
 Explanation.
+
Addition (or concatenation ) operator.  
- Subtraction operator.
* Multiplication operator.
/ Division operator.
% Remainder operator.
 
The complete program is listed below.
 
Code Example:
  1. public class ArithimeticOperators {    
  2.     public static void main(String[] args) {    
  3.         int p = 25;    
  4.         int q = 15;    
  5.         int res = p + q;    
  6.         System.out.println("p + q = " + res);    
  7.         res = p - q;    
  8.         System.out.println("p - q = " + res);    
  9.         res = p * q;    
  10.         System.out.println("p * q = " + res);    
  11.         res = p / q;    
  12.         System.out.println("p / q = " + res);    
  13.         res = p % 2;    
  14.         System.out.println("p % 2 = " + res);    
  15.     }    
  16. }     
  17.    
The output of the following code generates the following output.
 
 arthiPNG
 

3) Java Relational Operator. 

 
Relational operators are used to judge against the two operands and give result in a boolean form. The following table lists all the relational operators. There are two types of Relational operators-
 
Equality operators
 
The equality operators are the java relational operators determine if one operand is equal to the other operand, it returns a boolean value as the result. These operators can be used to compare the objects references. The following table list of all the equality operators.
 
 Operators 
Explanation.
==
Equal to
!=
Not equal to

The complete program is listed below.
 
Code Example:
  1. public class EqualityOperators {        
  2.         
  3.     public static void main(String[] args) {        
  4.         int p = 25;        
  5.         int q = 35;        
  6.         boolean res ;        
  7.         res = p == q;        
  8.         System.out.println("p == q : " + res);        
  9.         res = p != q;        
  10.         System.out.println("p != q : " + res);                       
  11.     }        
  12. }      
The output of the above code generates the following output.
 
equal  

Comparison Operators

Java comparison operators are used in java for comparing the values of two operands. Like less than or greater than etc. The following table list of all the comparison operators.
 
 Operators   Explanation.
 >  greater than
 >=  greater than equal to
 <  less than
 <=  less than equal to
 
The complete program is listed below.
    Code Example:       
    1. public class ComparisonOperators {    
    2.     
    3.     public static void main(String[] args) {    
    4.         int p = 25;    
    5.         int q = 35;    
    6.         boolean res;    
    7.             
    8.         res = p > q;    
    9.         System.out.println("p is greater than q : " + res);    
    10.         res = p >= q;    
    11.         System.out.println("p is greater than or equal to q : " + res);    
    12.         res = p < q;    
    13.         System.out.println("p is less than q : " + res);    
    14.         res = p <= q;    
    15.         System.out.println("p is less than or equal to q : " + res);        
    16.     }    
    17. }    
    The output of the above code generates the following output.
     
    compe 
     
    InstanceOf Operators
     
    Instance operator is used as a comparison operator in java. This operator tests that object of a class is an instance of a class or not and gives the result in a boolean value. The following table list of all the instanceOf operator,

     Operator  Explanation.
     instanceOf  instanceOf operator checks the type of the object.

    The complete program is listed below.

    Code Example:
    1. public class InstanceOfOperator 
    2. {    
    3.     public static void main(String[] args) {  
    4.         String name = "Hello";  
    5.         if (name instanceof String) {  
    6.             System.out.println("Hello is an instance of  a String class");  
    7.         }  
    8.         else
    9.         {  
    10.             System.out.println("Hello is not an instance of  a String class");  
    11.         }  
    12.     }  
    13. }  
    The output of the above code generates the following output.
     
    ins

    4) Java Unary Operators.

     
    Java Unary Operators are engaged in a single operand. The following table lists Java unary operators-
     
     Operators
     Explanation.
     + Unary plus operator; indicates a positive value
     -  Unary minus operator; indicates a negative value
     ++
     Increment operator; increments a value by 1
     --  Decrement operator decrements a value by 1
     !  Logical complement operator; return a boolean value
           
    The complete program is listed below.
      
    Code Example:
    1. public class UnaryOperators {    
    2.     
    3.     public static void main(String[] args) {    
    4.         int p = 20;    
    5.         int res;    
    6.         System.out.println( "p : " +p);    
    7.         res = ++p;    
    8.         System.out.println("++p = " + res);    
    9.         res = p++;    
    10.         System.out.println("p++ : "+res);    
    11.         res = --p;    
    12.         System.out.println("--p = " + res);    
    13.         res = p--;    
    14.         System.out.println("p-- = " + res);    
    15.     }    
    16. }    
    The output of the above code generates the following output.
     
    unary
     

    5) Java Logical Operators.

      
    Logical operators are used to do logical operations in Java. It operates on two boolean values and returns a boolean value as a result. The following table lists Java logical operators-
     
     Operators 
     Explanation.
     &&  Logical AND operator;(When two operands are true then the condition gives true). 
     ||  Logical OR operator;(If any two operands are not true then the condition give true).
     1  Logical NOT operator;(It uses to reverse the result if the value is true it returns false vice versa).
           
    The complete program is listed below.
     
    Code Example:
    1. public class LogicalOperators 
    2. {        
    3.     public static void main(String[] args) {    
    4.         boolean p = true;    
    5.         boolean q = false;    
    6.         boolean res = (p && q);    
    7.         System.out.println("p && q = " + res);    
    8.         res = (p || q);    
    9.         System.out.println("p || q = " + res);    
    10.         res = !p;    
    11.         System.out.println("!p = " + res);    
    12.     }        
    13. }     

    The output of the above code generates the following output.
     
    log 
     

    6) Java Conditional Operators (Ternary Operators). 

     
    The Java conditional operators select one of two expressions for evaluation, which is based on the value of the first operands. It is also called ternary operators because it takes three arguments. The following table lists Java ternary operators- 
      
     Operator
     Explanation.
     ? : Ternary operator;(If the first operand is true then it returns second operand else it returns the third operand). 
       
    The complete program is listed below.
     
    Code Example:
    1. public class TernaryOperators 
    2. {        
    3.         public static void main(String[] args) {    
    4.             int p = 10;    
    5.             int q = 20;    
    6.             int res = (p > 10) ? p : q;    
    7.             System.out.println("result 1 is: " + res);    
    8.             res = (q > 10) ? p : q;    
    9.             System.out.println("result 2 is: " + res);    
    10.         }    
    11.     }     
    The output of the above code generates the following output.
     
    ter
     

    7) Java Bitwise Operators.

     
    Bitwise operators perform bit shift operation on integral types only and not other types. The following table lists bitwise operators-
     
     Operators  Explanation
    ~ unary bitwise complement; inverts a bit pattern
    << signed left shift
    >> signed right shift
    >>> unsigned right shift
    & bitwise exclusive AND
    ^ bitwise exclusive XOR
    |       bitwise exclusive OR
     
    The complete program is listed below.
     
    Code Example
    1. public class BitwiseOper {  
    2.     public static void main(String[] args) {  
    3.         //Initial values  
    4.         int a = 5;  
    5.         int b = 7;  
    6.         // bitwise and  
    7.         // 0101 & 0111=0101 = 5  
    8.         System.out.println("a&b = " + (a & b));  
    9.   
    10.         // bitwise or  
    11.         // 0101 | 0111=0111 = 7  
    12.         System.out.println("a|b = " + (a | b));  
    13.   
    14.         // bitwise xor  
    15.         // 0101 ^ 0111=0010 = 2  
    16.         System.out.println("a^b = " + (a ^ b));  
    17.   
    18.         // bitwise and  
    19.         // ~0101=1010  
    20.         // will give 2's complement of 1010 = -6  
    21.         System.out.println("~a = " + ~a);  
    22.   
    23.         // can also be combined with  
    24.         // assignment operator to provide shorthand  
    25.         // assignment  
    26.         // a=a&b  
    27.         a &= b;  
    28.         System.out.println("a= " + a);  
    29.     }  
    30. }  
    The output of the above code generates the following output.
     
    bit

    Summary

     
    In this tutorial, we learned about various java operators and how to use them in our Java programs to perform some operations.