Operators in Java

Last updated on:

In the previous pages, we have learnt how to initialize and declare a variable and also about the data types.

Well, You probably want to know how to do something with single or multiple variables.

Now, it’s time to learn how to use operators to manipulate variables.

Here we go.

What are Operators?

Operators are the special type of symbols which is used to carry out the operations on operands (i.e. variables).

Some operators are: +, -, %, * or += etc.

For example:

var=num1+num2*num3;

Here, num1, num2, and num3 are the operands (i.e. variables).

+ and * are the operators to carry the addition and multiplication.

and, var is the other variable that stores the result after the operation gets performed.

Types of Operators

In Java, different types of operators are:

  • Unary Operator
  • Arithmetic Operator
  • Assignment Operator
  • Relational Operator
  • Logical Operator
  • Bitwise Operator
  • Ternary Operator
  • Shift Operator

Unary Operator

The unary operator requires only one operand to perform operations i.e,

  • increment and decrement of value by one
  • negating the value
  • inverting a value of a boolean

Unary Increment and Decrement (++, –)

Unary Increment (++)

Increment operators are used for the increment of a value by one.

It can be done in two ways:

1. Post-Increment Operator (a++):

The post-increment operator is used to increment the variable value by 1 after assigning the value to the variable.

i.e. first it assigns the value then, increment it by one.

Example (for Post-Increment Operator):

//unary post-increment operator
public class operators {
	public static void main(String []args){
		int num=20;
		int newNum=num++;

		//First use the value then increment it
		//int newNum=20
		//num=num+1;  => num=21;

		System.out.println("Value of newNum : "+  newNum); //20
		System.out.println("Value of num: " +num); //20+1=21
	}
}
//Output: 
Value of newNum : 20
Value of num: 21

2. Pre-Increment Operator (++a):

The pre-increment operator is used to increment the variable value by 1 before assigning the value to the variable.

i.e, first increment the value by one then, assign it.

Example (for Pre-Increment operator):

// Unary pre-increment operator
public class operators {
	public static void main(String []args){
		int num=20;
		int newNum = ++num;

		//First increment the value then use it
		//num=num+1;  => num=21;
		//int newNum=21;

		System.out.println("Value of newNum : " + newNum); //20+1=21
		System.out.println("Value of num: " + num); //21
	}
}
//Output:
Value of newNum : 21
Value of num: 21

Unary Decrement (- -)

Decrement operators are used for the decrement of a value by one.

It can be done in two ways:

1. Post-Decrement Operator (a- -):

The post-decrement operator is used to decrement the variable value by 1 after assigning the value to the variable.

i.e. first it assigns the value then, decrement it by one.

Example (for Post-Decrement Operator):

// Unary post-decrement operator
public class operators {
	public static void main(String []args){
		int num=20;
		int newNum=num--;

		//First use the value then decrement it
		//int newNum=20
		//num=num-1;  => num=19;

		System.out.println("Value of newNum : "+  newNum); //20
		System.out.println("Value of num: " +num); //20-1=19
	}
}
//Output:
Value of newNum : 20
Value of num: 19

2. Pre-Decrement Operator (- -a):

The pre-decrement operator is used to decrement the variable value by 1 before assigning the value to the variable.

i.e. first decrement the value by one then, assign it.

Example (for Pre-Decrement Operator):

// Unary pre-decrement operator
public class operators {
	public static void main(String []args){
		int num= 20;
		int newNum = --num;

		//First decrement the value then use it
		//num=num-1;  => num=19;
		//int newNum=19;

		System.out.println("Value of newNum : " + newNum); //20-1=19
		System.out.println("Value of num: " + num); //19
	}
}
//Output:
Value of newNum : 19
Value of num: 19

Arithmetic Operator

Arithmetic operators are used to perform the operation like addition, subtraction etc. There are 5 arithmetic operators:

  • Addition, +
  • Subtraction, –
  • Multiplication, *
  • Division, /
  • Remainder, %

Example 1 (for Arithmetic Operator):

// 1. Program to illustrate Arithmetic operators(with two operands)
public class operators {
	public static void main(String []args){
		int a=20;
		int b=10;
	
		System.out.println("Addition is: " +(a+b)); //20+10=30
		System.out.println("Subtraction is: " +(a-b)); //20-10=10
		System.out.println("Multiplication is: " +(a*b)); //20*10=200
		System.out.println("Division is: " +(a/b)); //20/10=2
		System.out.println("Modulus is: " +(a%b)); //20%10=0
	}
}
//Output:
Addition is: 30
Subtraction is: 10
Multiplication is: 200
Division is: 2
Modulus is: 0

Example 2 (for Arithmetic Operator):

// 2. Arithmetic operators(an expression)
public class operators {
	public static void main(String []args){
		int result;
		result=(2*3-6/2+8%4);
		System.out.println("Result of an expression is: "+result);
	}
}
//Output:
Result of an expression is: 3

Assignment Operator

The very basic assignment operator is = (equal to). It is used in Java to assign values to variables.

It is used to assign the value of the right to the variable on the left. For example:

int year;
year=2020;

Here, the value 2019 on the right side gets assigned to the variable year on the left side.

Apart from this, there are various general-purpose assignment operators as illustrated in the program given below.

Example for Assignment Operator:

// Program to illustrate assignment operators
public class operators {
	public static void main(String []args){
		int x=20;

		System.out.println("x+=5 is: "+ (x+=5));  //x=x+5
		System.out.println("x-=5 is: "+(x-=5)); //x=x-5
		System.out.println("x/=5 is: "+(x/=5)); //x=x/5
		System.out.println("x*=5 is: "+(x*=5)); //x=x*5
		System.out.println("x%=5 is: "+(x%=5)); //x=x%5
		System.out.println("x>>=5 is: "+(x>>=5)); //x=x>>5
		System.out.println("x<<=5 is: "+(x<<=5)); //x=x<<5
		System.out.println("x&=5 is: "+(x&=5)); //x=x&5
		System.out.println("x^=5 is: "+(x^=5)); //x=x^5
		System.out.println("x|=5 is: "+(x|=5)); //x=x|5
		System.out.println("x>=5 is: "+(x>=5)); //x=x>5
		System.out.println("x<=5 is: "+(x<=5)); //x=x<5
	}
}
//Output:
x+=5 is: 25
x-=5 is: 20
x/=5 is: 4
x*=5 is: 20
x%=5 is: 0
x>>=5 is: 0
x<<=5 is: 0
x&=5 is: 0
x^=5 is: 5
x|=5 is: 5
x>=5 is: true
x<=5 is: true

Relational Operator

The Java Relational operators compare operands and determine the relationship between them.

The output of the operations is always boolean i.e. it returns the true or false.

In Java, there are a total of six relational operators as illustrated in the program.

Example for Relational Operator:

// Program to illustrate relational operators
public class operators {
	public static void main(String []args){
		int x=34;
		int y=40;

		//is equal to
		System.out.println("is x equal to y: "+(x==y));

		// is not equal to
		System.out.println("is x not equal to y: "+(x!=y));

		// Greater than
		System.out.println("is x greater than y: "+(x>y));

		// Less than
		System.out.println("is x less than y: "+(x<y));

		// Greater than equal to
		System.out.println("is x greater than equal to y: "+(x>=y));

		// Less than equal to
		System.out.println("is x less than equal to y: "+(x<=y));
	}
}
//Output:
is x equal to y: false
is x not equal to y: true
is x greater than y: false
is x less than y: true
is x greater than equal to y: false
is x less than equal to y: true

Logical Operator

These operators are used to perform logical “AND”, “OR” and “NOT” operations, i.e. the function similar to AND gate and OR gate in digital electronics.

The logical operator in Java always returns the boolean value.

In Java, there are three logical operators as illustrated below in the program.

Example for the logical operator:

// Program to illustrate logical operators 
public class operators {
	public static void main(String []args){
		int x=20;
		int y=40;

		//logical -OR
		System.out.println("Logical OR operator: " +((x>40)||(y==40)));

		//logical -AND
		System.out.println("Logical AND operator: " +((x>40)&&(y==40)));

		//logical -NOT
		System.out.println("Logical NOT operator:" +!((x>40)||(y==40)));
	}
}
//Output:
Logical OR operator: true
Logical AND operator: false
Logical NOT operator: false

Bitwise Operator

Bitwise operators are used to perform manipulation of individual bits of a number.

Following are the basic bitwise operators used in Java:

//Bitwise OR(|):
Suppose we have to find bitwise OR between 5 and 7
5 =>   0101
7 =>   0111
(5|7)  0111 (In decimal=7)
//Bitwise AND(&):
Suppose we have to find bitwise AND between 5 and 7
5 =>   0101
7 =>   0111
(5&7)  0101 (In decimal=5)
//Bitwise XOR(^):
Suppose we have to find bitwise XOR between 5 and 7
5 =>   0101
7 =>   0111
(5^7)  0010 (In decimal=2)
//Bitwise Complement(~):
Suppose we have to find bitwise Complement between 5 and 7
7 =>   0111
(~7)   1000 (In decimal=8)
Compiler will give the 2's complement i.e, 2's complement of 8 = (-8)

Example for Bitwise Operators:

// Program to illustrate bitwise operators
public class operators {
	public static void main(String []args){
		int x=5;
		int y=7;

		//Bitwise OR
		System.out.println("Bitwise OR operator: " +(x|y));

		//Bitwise AND
		System.out.println("Bitwise AND operator: " +(x&y));

		//Bitwise XOR
		System.out.println("Bitwise XOR operator: " +(x^y));

		//Bitwise Complement
		System.out.println("Bitwise Complement operator: " +(~y));
	}
}
//Output:
Bitwise OR operator: 7
Bitwise AND operator: 5
Bitwise XOR operator: 2
Bitwise Complement operator: -8

Ternary Operator

The ternary operator is a little bit tricky operator, But it is one of the very useful operators in Java.

Actually, the ternary operator is the replacement of the if-else statement.

They are called ternary operators because they work on three operands.

Syntax:

result=condition ? value1 : value2

Example for Ternary Operator:

//Program to illustrate ternary operator
public class operators {
	public static void main(String []args){
		int x=17;
		int y=12;
		int result= (x>y)? x:y;
		System.out.println("largest number is:"+result);
	}
}
//Output:
largest number is:17

Shift Operator

These operators are used to shift the bits of a number left or right by multiplying or dividing the number by 2 respectively.

In java there are two types of shift operator:

1. Right shift (>>)

Shift the bits of the number to the right by filling the 0s to the void at the left.

Similar can be done by dividing the number by 2 for the time you want to shift. And, The final result will be the output produced at the last loop.

// Right-shift
Suppose we have a number(x)=17 and do x>>2.
17 = 00010001 (in binary)
1st shift= 00001000
2nd shift= 00000100
So, answer will be 00000100 i.e, 4 when we do x>>2.

OR,

for 17>>2,
1st shift= 17/2=8
2nd shift= 8/2=4
i.e, answer will be 4.

2. Left shift (<<)

Shift the bits of the number to the left by filling the 0s to the void at the right.

Similar can be done by multiplying the number by 2 for the time you want to shift. And, The final result will be the output produced at the last loop.

// Left-shift
Suppose we have a number(x)=15 and do x<<2.
15 = 00001111
 (in binary)
1st shift= 00011110
2nd shift= 00111100
So, answer will be 00111100 i.e, 60 when we do x<<2.

OR,

for 15<<2,
1st shift= 15*2=30
2nd shift= 30*2=60 
i.e, answer will be 60.

Example for Shift Operators:

// Program to illustrate shift operators
public class operators {
	public static void main(String []args){
		int x=17;
		int y=15;

		//Right Shift
		System.out.println("Right Shift operator: " +(x>>2));

		//Left Shift
		System.out.println("Left Shift operator: " +(y<<2));
	}
}
//Output:
Right Shift operator: 4
Left Shift operator: 60

Comments

Leave a Reply

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