Operators in Java
-
Last Updated: September 8, 2023
-
By: javahandson
-
Series
In this article, we will learn what is an Operator and the types of Operators in Java with proper examples. If you want to learn about other Java topics you can click the above Java menu or you can click on this link Java topics.
An operator is a symbol that performs some operations like Arithmetic or logical operations etc. An operator acts on the operands to get the desired results. These operands are the variables in Java.
There are mainly 8 types of operators. We will cover the top 4 types of operators in this article the rest of the operators will be covered in different articles. Please check the link here.
Arithmetic Operators are used to perform arithmetic operations. There are 5 arithmetic operators that are mentioned below in the table. To understand these operators we will take 2 variables int a = 20 and int b = 7 to perform the arithmetic operations.
Operator | Operator Name | Purpose | Example | Output |
+ | Addition | Used to add 2 operands | a+b | 27 |
– | Subtraction | Used to subtract 2 operands | a-b | 13 |
* | Multiplication | Used to multiply 2 operands | a*b | 140 |
/ | Division | Used to divide 2 operands and print the quotient | a/b | 2 |
% | Modulus | Used to divide 2 operands and print the remainder | a%b | 6 |
package com.java.handson; public class ArithmeticOperators { public static void main(String[] args) { int a = 20; int b = 7; System.out.println("Addition of 20 and 7 numbers : "+(a+b)); System.out.println("Subtraction of 20 and 7 numbers : "+(a-b)); System.out.println("Multiplication of 20 and 7 numbers : "+(a*b)); System.out.println("Division of 20 and 7 numbers : "+(a/b)); System.out.println("Modulus of 20 and 7 numbers : "+(a%b)); } } Output: Addition of 20 and 7 numbers : 27 Subtraction of 20 and 7 numbers : 13 Multiplication of 20 and 7 numbers : 140 Division of 20 and 7 numbers : 2 Modulus of 20 and 7 numbers : 6
Arithmetic operator acts on 2 operands at a time hence they are called binary operators.
The addition operator in Java is also used to concatenate 2 Strings to give a resultant output String.
package com.java.handson; public class ArithmeticOperators { public static void main(String[] args) { String str1 = "Java"; String str2 = "HandsOn"; String result = str1 + str2; System.out.println("Resultant output String : "+ result); } } Output : Resultant output String : JavaHandsOn
Relational operators are used to perform the comparison checks between 2 entities. The resultant value of the relational operation is a boolean i.e. true or false. As the relational operators are used for comparison they are also known as comparison operators.
The main use of relational operators is in the construction of conditions in statements like below:
if (condition is true) { execute the set of statements } else { execute some other set of statements }
There are 6 relational operators that are mentioned below in the table. To understand these operators we will take 3 variables
int a = 20, int b = 7 and int c = 20 to perform the relational operations.
Operator | Operator Name | Purpose | Example | Output |
> | Greater than | Evaluate if the value of the left operand is greater than the value of the right operand. If the value of the left operand is greater than the value of the right operand then the > operator returns true | a>b | true |
>= | Greater than or equal to | Evaluate if the value of the left operand is greater than or equal to the value of the right operand. If the value of the left operand is greater than or equal to the value of the right operand then the >= operator returns true | a>=c | true |
< | Lesser than | Evaluate if the value of the left operand is lesser than the value of the right operand. If the value of the left operand is lesser than the value of the right operand then the < operator returns true | a<b | false |
<= | Lesser than or equal to | Evaluate if the value of the left operand is lesser than or equal to the value of the right operand. If the value of the left operand is lesser than or equal to the value of the right operand then the <= operator returns true | a<=c | true |
== | Equal to | Evaluate if the value of the left operand is equal to the value of the right operand. If the value of the left operand is equal to the value of the right operand then the == operator returns true | a==c | true |
!= | Not Equal to | Evaluate if the value of the left operand is not equal to the value of the right operand. If the value of the left operand is not equal to the value of the right operand then the != operator returns true | a!=c | false |
package com.java.handson; public class RelationalOperators { public static void main(String[] args) { int a = 20; int b = 7; int c = 20; if (a>b) { System.out.println("Greater than operator returns true as 20>7"); } if (a>=c) { System.out.println("Greater than or equal to operator returns true as 20>=20"); } if (a<b) { System.out.println("Lesser than operator returns true as 20<7"); } else { System.out.println("Lesser than operator returns false as 20<7"); } if (a<=c) { System.out.println("Lesser than or equal to operator returns true as 20<=20"); } if (a==c) { System.out.println("Equal to operator return true as 20==20"); } if (a!=c) { System.out.println("Not Equal to operator return true as 20!=20"); } else { System.out.println("Not Equal to operator return false as 20!=20"); } } } Output: Greater than operator returns true as 20>7 Greater than or equal to operator returns true as 20>=20 Lesser than operator returns false as 20<7 Lesser than or equal to operator returns true as 20<=20 Equal to operator return true as 20==20 Not Equal to operator return false as 20!=20
Logical Operators are used to perform logical operations. Here we can combine multiple conditions together to give a resultant output. The output will be in the form of a Boolean i.e. true or false.
Logical operators are of 3 types:
Operator | Operator Name | Purpose |
&& | and | If both condition matches then return a true |
|| | or | If either of the condition matches then return a true |
! | not | It is used to reverse the state. Like if the output of the condition is true then not operator make the result false and if the output of the condition is false then not operator make the result true. |
package com.java.handson; public class LogicalOperators { public static void main(String[] args) { int value = 10; int x = 5; int y = 8; if (x<value && y<value) { System.out.println("&& -> Both the condition matches hence we are inside the loop"); } if (x<value || y>value) { System.out.println("|| -> Either one of the condition matches hence we are inside the loop"); } if(!(x>value)) { System.out.println("Reversing the state. x is not greater than value still we are inside the loop"); } } } Output: && -> Both the condition matches hence we are inside the loop || -> Either one of the condition matches hence we are inside the loop Reversing the state. x is not greater than value still we are inside the loop
The assignment operator is used to assign some value to a variable.
= is an assignment operator. We will see this operator almost everywhere in our Java programs. There are 3 ways in which we can use this assignment operator.
We will assign a value to a variable.
int a = 20;
We can also assign one variable to another that in turn assign the value.
int a = 20; int b = a; System.out.print("Value of b is : "+b); Output : Value of b is : 20
Here we can use a binary operator with an assignment operator that changes the value of the variable.
Binary operators can be either of + – * / % << >> etc.
int a = 20; a += 10; // This expression is equivalent to a = a + 10 System.out.print("Value of a is : "+a); Output : Value of a is : 30
int a = 2; a *= 10; // This expression is equivalent to a = a * 10 System.out.print("Value of a is : "+a); Output : Value of a is : 20
Firstly we will solve an expression on the right side of the assignment operator and then assign the resultant value of the expression to the variable on the left side.
int b = 5; int c = 7; int a = (2+b)*c; // (2+b)*c expression will be solved first and the resultant output will be assigned to variable a. System.out.print("Value of a is : "+a); Output : Value of a is : 49
package com.java.handson; public class AssignmentOperators { public static void main(String[] args) { int a = 20; System.out.println("Value of a is : " + a); int b = a; System.out.println("Value of b is : " + b); int c = 10; c += 20; // c = c + 20 System.out.println("Value of c is : " + c); int d = (2+b)*c; // d = (2+20)*30 = 22*30 System.out.println("Value of d is : " + d); } } Output : Value of a is : 20 Value of b is : 20 Value of c is : 30 Value of d is : 660
So this is all about the top 4 Operators in Java. You can check the other operators by clicking the link here. I hope you like the article. If you are having any questions on this topic please raise them in the comments section.