Types of Operators in Java with example

In the previous tutorial, we learned about Variables in Java Programming. In this tutorial, we will learn Types of Operators in Java with example

358

In this article, we will discuss types of Operators in Java (Arithmetic, Unary, Assignment, Relational, Logical, Ternary, Bitwise, Shift) with detailed examples and syntax.

Operators basically refer to symbols that can be used to perform operations. Examples are +, -, *, / etc. Java provides many different kinds of operators. They are put into different categories according to their work.

Types of Operators in Java

  1. Arithmetic Operators
  2. Unary Operators
  3. Assignment Operators
  4. Relational Operators
  5. Logical Operators
  6. Ternary Operators
  7. Bitwise Operators
  8. Shift Operators
Types of operators in Java with example
Types of operators in Java with example

The operators that are available in Java are discussed briefly here.

1. Arithmetic Operators –

They are used to perform simple operations that we have generally learned in our mathematics. The operators listed in this category are –

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
public class  Example{  
public static void main(String args[]){  
int a=10;  
int b=5;  
System.out.println(a+b);//15  
System.out.println(a-b);//5  
System.out.println(a*b);//50  
System.out.println(a/b);//2  
System.out.println(a%b);//0  
}}

Output –

15
5
50
2
0

2. Unary Operators –

Those operators that need only a single operand to operate are called unary operators. The operands in this category are –

  • Unary Minus: This operator is used to take the negative of a value
  • Unary Plus: This operator is used to indicate a positive value. However, even if it is not mentioned, the number is by default positive. If this operand is applied on byte, char, it performs an automatic conversion to int.
  • Increment operator: It increments the value by one. Further, there are two types of Increment operators.
    1. Post-Increment – First the result is computed, then incremented
    2. Pre-Increment – Value is incremented, and the result is computed.
  • Decrement Operator – It decrements the value by one. Further, it is also divided into two types i.e., post and pre-functioning which we have discussed above.
public class Example{  
public static void main(String args[]){  
int x=10;  System.out.println(x++);//10 -> (11)  
System.out.println(++x);//12  
System.out.println(x--);//12 -> (11)  
System.out.println(--x);//10  
}}

 

Output –

10
12
12
10

 

3. Assignment Operator –

This operator is used to assign a value to a variable. Using this operator, the value on the right side of the operator is assigned to the variable on the left side of the operator.

For example –

a = 5

In this, the value 5 is assigned to a.

The assignment operators can also be combined with some other operators to generate the short form. We will understand this properly with the given example –

a+=5 : This is the short form that we can use instead of a = a+5.

Similarly, we can use -=, *=, /=, %=.

public class Example{  
public static void main(String args[]){  
int a=10;  
int b=20;  
a+=4;//a=a+4 (a=10+4)  
b-=4;//b=b-4 (b=20-4)  
System.out.println(a);  
System.out.println(b);  
}}

 

Output –

14
16

4. Relational Operators –

These operators are used to check the relation or compare two variables. They return Boolean results true of false after the comparison. They are mostly used with if else statements for comparison. Some of the relational operators are listed below –

  • ==, This operator returns true if the value/expression at the left-hand side of the operator is equal to the right-hand side.
  • ! –, This operator returns true if the value/expression at the left-hand side of the operator is equal to the right-hand side.
  • <, This is the same as the conventional less than the operator. This returns true if the left-hand side of the operator is less than the right-hand side.
  • <=, This returns true if the left-hand side of the operator is less than the right-hand side, or even if they are equal.
  • >, This returns true if the left-hand side of the operator is greater than the right-hand side of the operator.
  • >=, This returns true if the left-hand side of the operator is greater than, or equal to the right-hand side.
public class Example{  
public static void main(String args[]){  
int a  = 10;
int b = 20;
System.out.println(a==b); // true}}

Output – false;

  1. Logical Operators –

These operators are similar to AND Gate and OR Gate in digital electronics. The logical operators are defined below –

  • && – This is Logical AND operator. This returns a true value if both the conditions are true.
  • || – This is the Logical OR operator. This returns a true value even if one of the conditions is true.
  • ! – This is logical NOT operator. This returns a true value when the condition is false, or returns false when a condition is true.
public class Example{  
public static void main(String args[]){  
int a=10;  
int b=5;  
int c=20;  
System.out.println(a<b&&a<c);//false && true = false  
}}

Output – false

  1. Ternary Operator –

This operator is a replacement for the if else statement. The format of the ternary operator is –

(condition) ? statement 1 : statement 2

First, the condition is checked. If it evaluates to true, the first statement is run, and if it evaluates to false, the second statement is run

public class Example{  
public static void main(String args[]){  
int a=10;  
int b;
int c = 15;
a == b? b = 25 : b = 10;
system.out.println(b);
}}

Output – 10 (because the second statement is run)

 

  1. Bitwise Operator –

This operator is the same as the logical operators. The difference between logical and bitwise operators is that logical operators work on Boolean expressions and returns true or false. Whereas, bitwise operators work on binary digits of integer values and return an integer.

            They are of four types –

  • Bitwise AND – Returns bit by bit AND of inputs
  • Bitwise OR – Returns bit by bit OR of inputs
  • Bitwise XOR – Returns bit by bit XOR of inputs
  • Bitwise ~ – Returns one’s complement of the input value
public class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=5;  
int c=20;  
System.out.println(a<b&a<c);//false & true = false  
}}

Output – false

  1. Shift Operators –

These operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two. The types of shift operators are –

  • << , Left shift operator – This operator shifts the bits of the number towards left and inserts 0 from right.
  • >>, Signed right shift operator – This operator shifts the bit of the number to right and inserts 0 from left.
  • >>>, Unsigned right shift operator – This operator shifts the bits of the number to right and inserts 0 on left. Also, the leftmost bit is set to 0.
public class Example{  
public static void main(String args[]){  
System.out.println(10<<2);//10*2^2=10*4=40  
System.out.println(10<<3);//10*2^3=10*8=80  
System.out.println(20<<2);//20*2^2=20*4=80  
System.out.println(15<<4);//15*2^4=15*16=240  
}}

Output –

40
80
80
240

So, these articles provide basic information about the categories and types of operators. Further, we will understand the working of these while learning programming in upcoming articles.

Time to Attempt a short Quiz on Operators in Java

Previous articleList of Python keywords with detailed examples
Next articleA Step-by-Step Guide to Generate a Short Summary Using an Online Tool
Harshit Brijwasi
I am Harshit Brijwasi. I am an engineer and a Technical Content writer. I am here to share my knowledge about technical topics and help researchers with them.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.