Browse by Domains

What are Java Operators? Types, Examples and more

What are Java Operators?

Java operators are symbols that are used to perform operations on variables and manipulate the values of the operands. Each operator performs specific operations. Let us consider an expression 5 + 1 = 6; here, 5 and 1 are operands, and the symbol + (plus) is called the operator. We will also learn about operator precedence and operator associativity. 

Types of Java Operators:

  1. Unary Operators
  2. Arithmetic Operators
  3. Assignment Operators
  4. Logical Operators
  5. Shift Operators
  6. Bitwise Operators
  7. Ternary Operators
  8. Relational Operators

Let us see each operator one by one in detail.

  1. Unary Operator: Unary operator requires only a single operand; this operator is used to increment or decrement the value, negating an expression or inverting a boolean value.
OperatorDescription
++ (Increment) This operator is used to increment the value by 1. There are two types of increment, i.e., post-increment (a++) and pre-increment (++a)
— (Decrement).This operator is used to decrement the value by 1. There are two types of decrement, i.e., post decrement (a–) and pre decrement (–a)
! (Invert)This operator is used to invert a boolean value (!a).

Example:

public class GreatLearning {
public static void main ( String[] args ) {  
int a = 10;  
boolean b = True;
System.out.println ( a++ );
System.out.println ( ++a );
System.out.println ( a-- );  
System.out.println ( --a );
System.out.println ( !b );
}
}

Output:

10

12

12

10

False

  1.  Arithmetic Operator: Arithmetic operators are used to performing addition, subtraction, multiplication, division, and modulus. It acts as a mathematical operations.
OperatorDescription
+ ( Addition )This operator is used to add the value of the operands.
– ( Subtraction )This operator is used to subtract the right-hand operator with the left hand operator.
* ( Multiplication )This operator is used to multiply the value of the operands.
/ ( Division )This operator is used to divide the left hand operator with right hand operator.
% ( Modulus )This operator is used to divide the left hand operator with right hand operator and returns remainder. 

Example:

public class GreatLearning {  
public static void main ( String[] args ) {  
int a = 10;  
int b = 20;  
System.out.println ( a + b );  
System.out.println ( a – b );  
System.out.println ( a * b );  
System.out.println ( a / b );  
System.out.println ( a % b );
}
}

Output:

30

-10

200

0

10

  1. Assignment Operator :  Assignment operator are used to assign new value to a variable. The left side operand of the assignment operator is called variable and the right side operand of the assignment operator is called value. 
OperatorDescription
=This operator is used to assign the value on the right to the operand on the left.
+=This operator is used to add right operand to the left operand and assigns the result to the left operand.
-=This operator subtracts right operand from the left operand and assigns the result to the left operand.
*=This operator multiplies right operand with the left operand and assigns the result to the left operand.
/=This operator divides left operand with the right operand and assigns the result to the left operand.
^=This operator performs exponential calculation on operators and assigns value to the left operand
%=This operator is used to divide the left-hand operator with right hand operator and assigns the result to left operand.

(!b); // returns false

Example:

public class GreatLearning {
public static void main ( String[] args ) {
int a = 10;
int b = 20;
int c;
System.out.println ( c = a ); 
System.out.println ( b += a );
System.out.println ( b -= a);
System.out.println ( b *= a );
System.out.println ( b /= a );
System.out.println ( b ^= a );
System.out.println ( b %= a );
}
}

Output:

10

30

10

200

2

0

0

  1. Logical Operator: Logical operators are used to combining two or more conditions or complement the evaluation of the original condition under consideration.
OperatorDescription
&& (Logical AND)This operator returns True if both the operands are true, otherwise, it returns False.
|| (Logical OR)This operator returns True if either the operands are true, otherwise it returns False.
! (Logical AND)This operator returns True if an operand is False. It reverses the logical state of an operand.

Example: 

public class GreatLearning {
public static void main ( String args[] ) {
             int a = 5;
             System.out.println ( a<5  &&  a<20 );
             System.out.println ( a<5 || a<20 );
             System.out.println ( ! ( a<5  &&  a<20 ));
}  
}

Output:

false

true

true

  1. Shift Operator: The shift operator is used to shift the bits of a number left or right by multiplying or dividing the numbers. 
OperatorDescription
<< (Left Shift)This operator left shifts the bits of the first operand; the second operand decides the number of places to shift.
>> (Right Shift)This operator right shifts the bits of the first operand; the second operand decides the number of places to shift.

Example :

public class GreatLearning {
      	public static void main ( String args[] ) {
            	int a = 58; 
        	System.out.println ( a<<2 ); 
        	System.out.println ( a>>2 ); 
	}
	}

Output :

232

14

  1. Bitwise Operator: The bitwise operator operates on bit string, binary number, or bit array. It is fast and simple and directly supported by the processor. The bitwise operation is also known as bit-level programming. 
OperatorDescription
& (Bitwise AND)This operator takes two numbers as operands and does AND on every  bit of two numbers. 
| (Bitwise OR)This operator takes two numbers as operands and does OR on every  bit of two numbers.
^ (Bitwise XOR)This operator takes two numbers as operands and does XOR on every  bit of two numbers.
~ (Bitwise NOT)This operator takes one number as an operand and does invert all bits of that number.

Example :

public class GreatLearning {
public static void main( String[] args ) {
int a = 58; 
            int b = 13; 
        	System.out.println ( a&b );  
       	System.out.println ( a|b );  
        	System.out.println ( a^b );  
        	System.out.println ( ~a );  
	}
	}

Output :

8

63

55

-59

  1. Ternary Operator : Ternary operator is an conditional operator, it reduces the line of code while performing the conditional or comparisons. It is the replacement of if-else or nested if-else statements. It is also referred to as inline if, conditional operator, or ternary if. 

Syntax : ( Condition ) ? ( Statement1 ) : ( Statement2 );

Here Condition is the expression to be evaluated which will return the boolean value. If the Condition result is True then Statement1 will be executed. If the Condition result is false then Statement2 will be executed. 

Example : 

public class GreatLearning {  
public static void main ( String args[] ) {  
int a = 4;  
int b = 9;  
int min = ( a<b ) ? a : b;  
System.out.println ( min );  
}
}  

Output : 

4

  1. Relational Operator: Relational operator compares two numbers and returns a boolean value. This operator is used to define a relation or test between two operands.
OperatorDescription
< (Less than)This operator returns True, if the value of the left operand is less than the value of the right operand, else it returns False.
> (Greater than)This operator returns True, if the value of the left operand is greater than the value of the right operand, else it returns False.
<= (Less than or equal to)This operator returns True, if the value of the left operand is less than or equal to the value of the right operand, else it returns False.
>= (Greater than or equal to)This operator returns True, if the value of the left operand is greater than or equal to the value of the right operand, else it returns False.
== (Equal to)This operator returns True, if two operands are equal, else it returns False.
!= (Not equal to)This operator returns True, if two operands are not equal, else it returns False.

Example : 

public class GreatLearning {
      	public static void main ( String[] args ) {
            	int  a = 10;
            	int  b = 20;
            	System.out.println ( a < b ); 
            	System.out.println(  a > b ); 
            	System.out.println ( a <= b ); 
            	System.out.println (a >= b ); 
             System.out.println ( a == b ); 
             System.out.println ( a != b ); 
             }
             }

Output : 

true

false

true

false

false

true

Java Operator Precedence and Associativity

Operator Precedence : Operator precedence determines which operator is performed first in an expression if there is more than one operators with different precedence. 

Operator Associativity : Operator associativity is used when an expression have two operators having same precedence.

OperatorPrecedenceAssociativity
Postfixexpr++  exprleft to right
Unary++expr —expr +expr –expr ~ !right to left
Multiplicative* / %left to right
Additive+ –left to right
Shift<< >> >>>left to right
Relational< > <= >= instanceofleft to right
Equality== !=left to right
bitwise AND&left to right
bitwise exclusive OR^left to right
bitwise inclusive OR|left to right
logical AND&&left to right
logical OR||left to right
Ternary? :right to left
Assignment= += -= *= /= %= &= ^= |= <<= >>= >>>=right to left

Conclusion

Overall, operators are an important part of Java and are necessary for performing various operations on data. There are many different types of operators available in Java, each with its own specific purpose. While some operators are used more frequently than others, all of them can be useful in the right situation. With a little practice, anyone can become proficient in using Java operators.

Engaging in the study of Java programming suggests a keen interest in the realm of software development. For those embarking upon this journey with aspirations towards a career in this field, it is recommended to explore the following pages in order to acquire a comprehensive understanding of the development career path:

Software engineering courses certificates
Software engineering courses placements
Software engineering courses syllabus
Software engineering courses fees
Software engineering courses eligibility
Avatar photo
Great Learning
Great Learning's Blog covers the latest developments and innovations in technology that can be leveraged to build rewarding careers. You'll find career guides, tech tutorials and industry news to keep yourself updated with the fast-changing world of tech and business.

Leave a Comment

Your email address will not be published. Required fields are marked *

Great Learning Free Online Courses
Scroll to Top