Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide. In this article, we will learn about Java Operators and learn all their types.
What are the Java Operators?
Operators in Java are the symbols used for performing specific operations in Java. Operators make tasks like addition, multiplication, etc which look easy although the implementation of these tasks is quite complex.
Types of Operators in Java
There are multiple types of operators in Java all are mentioned below:
1. Arithmetic Operators
They are used to perform simple arithmetic operations on primitive data types.
// Java Program to implement
// Arithmetic Operators
import java.io.*;
// Drive Class
class Test {
// Main Function
public static void main (String[] args) {
// Arithmetic operators
int a = 10;
int b = 3;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
}
}
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
2. Unary Operators
Unary operators need only one operand. They are used to increment, decrement, or negate a value.
// Java Program to implement
// Unary Operators
import java.io.*;
// Driver Class
class Test {
// main function
public static void main(String[] args)
{
// Interger declared
int a = 10;
int b = 10;
// Using unary operators
System.out.println("Postincrement : " + (a++));
System.out.println("Preincrement : " + (++a));
System.out.println("Postdecrement : " + (b--));
System.out.println("Predecrement : " + (--b));
}
}
Postincrement : 10
Preincrement : 12
Postdecrement : 10
Predecrement : 8
3. Assignment Operator
‘=’ Assignment operator is used to assign a value to any variable. It has right-to-left associativity, i.e. value given on the right-hand side of the operator is assigned to the variable on the left, and therefore right-hand side value must be declared before using it or should be a constant.
The general format of the assignment operator is:
variable = value;
In many cases, the assignment operator can be combined with other operators to build a shorter version of the statement called a Compound Statement. For example, instead of a = a+5, we can write a += 5.
// Java Program to implement
// Assignment Operators
import java.io.*;
// Driver Class
class Test {
// Main Function
public static void main(String[] args)
{
// Assignment operators
int f = 7;
System.out.println("f += 3: " + (f += 3));
System.out.println("f -= 2: " + (f -= 2));
System.out.println("f *= 4: " + (f *= 4));
System.out.println("f /= 3: " + (f /= 3));
System.out.println("f %= 2: " + (f %= 2));
System.out.println("f &= 0b1010: " + (f &= 0b1010));
System.out.println("f |= 0b1100: " + (f |= 0b1100));
System.out.println("f ^= 0b1010: " + (f ^= 0b1010));
System.out.println("f <<= 2: " + (f <<= 2));
System.out.println("f >>= 1: " + (f >>= 1));
System.out.println("f >>>= 1: " + (f >>>= 1));
}
}
f += 3: 10
f -= 2: 8
f *= 4: 32
f /= 3: 10
f %= 2: 0
f &= 0b1010: 0
f |= 0b1100: 12
f ^= 0b1010: 6
f <<= 2: 24
f >>= 1: 12
f >>>= 1: 6
4. Relational Operators
These operators are used to check for relations like equality, greater than, and less than. They return boolean results after the comparison and are extensively used in looping statements as well as conditional if-else statements. The general format is,
variable relation_operator value
Some of the relational operators are-
// Java Program to implement
// Relational Operators
import java.io.*;
// Driver Class
class Test {
// main function
public static void main(String[] args)
{
// Comparison operators
int a = 10;
int b = 3;
int c = 5;
System.out.println("a > b: " + (a > b));
System.out.println("a < b: " + (a < b));
System.out.println("a >= b: " + (a >= b));
System.out.println("a <= b: " + (a <= b));
System.out.println("a == c: " + (a == c));
System.out.println("a != c: " + (a != c));
}
}
a > b: true
a < b: false
a >= b: true
a <= b: false
a == c: false
a != c: true
5. Logical Operators
These operators are used to perform “logical AND” and “logical OR” operations, i.e., a function similar to AND gate and OR gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e., it has a short-circuiting effect. Used extensively to test for several conditions for making a decision. Java also has “Logical NOT”, which returns true when the condition is false and vice-versa
Conditional operators are:
// Java Program to implemenet
// Logical operators
import java.io.*;
// Driver Class
class GFG {
// Main Function
public static void main (String[] args) {
// Logical operators
boolean x = true;
boolean y = false;
System.out.println("x && y: " + (x && y));
System.out.println("x || y: " + (x || y));
System.out.println("!x: " + (!x));
}
}
x && y: false
x || y: true
!x: false
6. Ternary operator
The ternary operator is a shorthand version of the if-else statement. It has three operands and hence the name Ternary.
The general format is:
condition ? if true : if false
The above statement means that if the condition evaluates to true, then execute the statements after the ‘?’ else execute the statements after the ‘:’.
// Java program to illustrate
// max of three numbers using
// ternary operator.
public class operators {
public static void main(String[] args)
{
int a = 20, b = 10, c = 30, result;
// result holds max of three
// numbers
result
= ((a > b) ? (a > c) ? a : c : (b > c) ? b : c);
System.out.println("Max of three numbers = "
+ result);
}
}
Max of three numbers = 30
7. Bitwise Operators
These operators are used to perform the manipulation of individual bits of a number. They can be used with any of the integer types. They are used when performing update and query operations of the Binary indexed trees.
// Java Program to implement
// bitwise operators
import java.io.*;
// Driver class
class GFG {
// main function
public static void main(String[] args)
{
// Bitwise operators
int d = 0b1010;
int e = 0b1100;
System.out.println("d & e: " + (d & e));
System.out.println("d | e: " + (d | e));
System.out.println("d ^ e: " + (d ^ e));
System.out.println("~d: " + (~d));
System.out.println("d << 2: " + (d << 2));
System.out.println("e >> 1: " + (e >> 1));
System.out.println("e >>> 1: " + (e >>> 1));
}
}
d & e: 8
d | e: 14
d ^ e: 6
~d: -11
d << 2: 40
e >> 1: 6
e >>> 1: 6
8. Shift Operators
These operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively. They can be used when we have to multiply or divide a number by two. General format-
number shift_op number_of_places_to_shift;
// Java Program to implement
// shift operators
import java.io.*;
// Driver Class
class GFG {
// main function
public static void main(String[] args)
{
int a = 10;
// using left shift
System.out.println("a<<1 : " + (a << 1));
// using right shift
System.out.println("a>>1 : " + (a >> 1));
}
}
a<<1 : 20 a>>1 : 5
9. instanceof operator
The instance of the operator is used for type checking. It can be used to test if an object is an instance of a class, a subclass, or an interface. General format-
object instance of class/subclass/interface
// Java program to illustrate
// instance of operator
class operators {
public static void main(String[] args)
{
Person obj1 = new Person();
Person obj2 = new Boy();
// As obj is of type person, it is not an
// instance of Boy or interface
System.out.println("obj1 instanceof Person: "
+ (obj1 instanceof Person));
System.out.println("obj1 instanceof Boy: "
+ (obj1 instanceof Boy));
System.out.println("obj1 instanceof MyInterface: "
+ (obj1 instanceof MyInterface));
// Since obj2 is of type boy,
// whose parent class is person
// and it implements the interface Myinterface
// it is instance of all of these classes
System.out.println("obj2 instanceof Person: "
+ (obj2 instanceof Person));
System.out.println("obj2 instanceof Boy: "
+ (obj2 instanceof Boy));
System.out.println("obj2 instanceof MyInterface: "
+ (obj2 instanceof MyInterface));
}
}
class Person {
}
class Boy extends Person implements MyInterface {
}
interface MyInterface {
}
obj1 instanceof Person: true
obj1 instanceof Boy: false
obj1 instanceof MyInterface: false
obj2 instanceof Person: true
obj2 instanceof Boy: true
obj2 instanceof MyInterface: true
1. Precedence and Associativity:
There is often confusion when it comes to hybrid equations which are equations having multiple operators. The problem is which part to solve first. There is a golden rule to follow in these situations. If the operators have different precedence, solve the higher precedence first. If they have the same precedence, solve according to associativity, that is, either from right to left or from left to right. The explanation of the below program is well written in comments within the program itself.
public class operators {
public static void main(String[] args)
{
int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30;
// precedence rules for arithmetic operators.
// (* = / = %) > (+ = -)
// prints a+(b/d)
System.out.println("a+b/d = " + (a + b / d));
// if same precedence then associative
// rules are followed.
// e/f -> b*d -> a+(b*d) -> a+(b*d)-(e/f)
System.out.println("a+b*d-e/f = "
+ (a + b * d - e / f));
}
}
a+b/d = 20
a+b*d-e/f = 219
2. Be a Compiler:
The compiler in our systems uses a lex tool to match the greatest match when generating tokens. This creates a bit of a problem if overlooked. For example, consider the statement a=b+++c; too many of the readers might seem to create a compiler error. But this statement is absolutely correct as the token created by lex is a, =, b, ++, +, c. Therefore, this statement has a similar effect of first assigning b+c to a and then incrementing b. Similarly, a=b+++++c; would generate an error as the tokens generated are a, =, b, ++, ++, +, c. which is actually an error as there is no operand after the second unary operand.
public class operators {
public static void main(String[] args)
{
int a = 20, b = 10, c = 0;
// a=b+++c is compiled as
// b++ +c
// a=b+c then b=b+1
a = b++ + c;
System.out.println("Value of a(b+c), "
+ " b(b+1), c = " + a + ", " + b
+ ", " + c);
// a=b+++++c is compiled as
// b++ ++ +c
// which gives error.
// a=b+++++c;
// System.out.println(b+++++c);
}
}
Value of a(b+c), b(b+1), c = 10, 11, 0
3. Using + over ():
When using the + operator inside system.out.println() make sure to do addition using parenthesis. If we write something before doing addition, then string addition takes place, that is, associativity of addition is left to right, and hence integers are added to a string first producing a string, and string objects concatenate when using +. Therefore it can create unwanted results.
public class operators {
public static void main(String[] args)
{
int x = 5, y = 8;
// concatenates x and y as
// first x is added to "concatenation (x+y) = "
// producing "concatenation (x+y) = 5"
// and then 8 is further concatenated.
System.out.println("Concatenation (x+y)= " + x + y);
// addition of x and y
System.out.println("Addition (x+y) = " + (x + y));
}
}
Concatenation (x+y)= 58
Addition (x+y) = 13
The advantages of using operators in Java are mentioned below:
The disadvantages of Operators in Java are mentioned below:
Now, let's check how much they remember from the last class with the help of quizzes.
Q1. What will be output for this ?
System.out.print("Welcome in playground")
Q2. What will be output for this ?
system.out.print("Hi Everyone");
Q3. What will be output for this ?
System.ouT.print("Hi Guys");
Q4. What will be output for this ?
System.out.print(Good Morning Everyone);
Q5. What will be output for this ?
System.out.print('Happy Thursday');
Q6. What will be output for this ?
System.out.print(10 + 20);
Q7. What will be output for this ?
System.out.print(10 - 25);
Q8. What will be output for this ?
System.out.println("Hello");
System.out.print("World);
Q9. What will be output for this ?
System.out.print("Hello");
System.out.println("World");
Q10. What will be output for this ?
System.out.println("Hello");
System.out.print("World");
System.out.println("Welcome")
Q11. What will be output for this ?
System.out.printLN("Hello");
System.out.println("World);
Q12. What will be output for this ?
Which of the follwing are operators?
Q13. What is the outup.?
10+30
In the given expression choose the operands.
Explanation of Quiz : Operator : + Operands : 10 and 30
Explain using one more example if needed.
Numbers -> 2 types
Q14. What will be output for this ?
System.out.print(5 + 8);
Q15. What will be output for this ?
System.out.print(5 - 8);
Q16. What will be output for this ?
System.out.print(5 * 8);
Q17 What will be output for this ?
System.out.print(8 / 2);
Q18. What will be output for this ?
System.out.print(10 / 3);
Explanation : In calculator, 10 / 3 = 3.33333. But in JAVA, we get 3 (round-off).
Rule : In JAVA, when you divide (/) integers we only get quotient.
Q19. What will be output for this ?
System.out.print(24 / 9);
Q20. What will be output for this ?
System.out.print(3 / 6);
Q21. What will be output for this ?
System.out.print(24 / 0);
Rule: Division of integers by zero is not possible in JAVA
Q22. What will be output for this ?
System.out.print(6 * 7 / 6);
Explanation:
But we cannot have two answers for the same expression. So, there must be some rules in place to get the output.
Explanation: Here, * and / have same priority. Because * comes first we will evaluate 6 * 7 will be evaluated first. => 6 * 7 / 6 => 42 / 6 => 7
Q1. What will be output for this ?
System.out.print(4 + 3 * 6 - 7 / 2);
Explanation: Between * and / we will be evaluating multiply first and then divide. => 4 + 3 * 6 - 7 / 2 => 4 + 18 - 7 / 2 => 4 + 18 - 3 => 22 - 3 => 19
Q2. What will be output for this ?
System.out.print(5 + 2 * 3);
Explanation: Here, * has higher priority. => 5 + 2 * 3 => 5 + 6 => 11
Q3. What will be output for this ?
System.out.print(5 + 15 / 5 + 6 * 3);
Explanation: => 5 + 15 / 5 + 6 * 3 => 5 + 3 + 6 * 3 => 5 + 3 + 18 => 8 + 18 => 26
Q4. What will be output for this ?
System.out.print(7 - 2 * 4 + 18 / 3);
Explanation: => 7 - 2 * 4 + 18 / 3 => 7 - 8 + 18 / 3 => 7 - 8 + 6 => -1 + 6 => 5
Q5. What will be output for this ?
System.out.print(3 * 4 / 2 + 7 + 3 - 4 / 2);
Explanation: => 3 * 4 / 2 + 7 + 3 - 4 / 2 => 12 / 2 + 7 + 3 - 4 / 2 => 6 + 7 + 3 - 4 / 2 => 6 + 7 + 3 - 2 => 13 + 3 - 2 => 16 - 2 => 14
Q6. What will be output for this ?
System.out.print(5 + 2 * 4 + 8 - 6 + 12 / 4);
Explanation: => 5 + 2 * 4 + 8 - 6 + 12 / 4 => 5 + 8 + 8 - 6 + 12 / 4 => 5 + 8 + 8 - 6 + 3 => 13 + 8 - 6 + 3 => 21 - 6 + 3 => 15 + 3 => 18
Q7. What will be output for this ?
System.out.print( (5 + 2) * 3 );
Explanation: => (5 + 2) * 3) => 7 * 3 => 21
Q8. What will be output for this ?
System.out.print("Hello" + "World");
Explanation:
Rule: With + operator, If one Operand is text then we concatenate both Operands
Q9. What will be output for this ?
System.out.print("Hi" + "Students" + "Namaste");
Explanation: => "Hi" + "Students" + "Namaste" => "HiStudents" + "Namaste" => "HiStudentsNamaste"
Q10. What will be output for this ?
System.out.print("Hi" + " " + "Namaste");
Explanation: => "Hi" + " " + "Namaste" => "Hi " + "Namaste" => "Hi Namaste"
Q11. What will be output for this ?
System.out.print("Hi" * "Guys");
Explanation: We cannot use * with text operand.
Rule: With text operand, only + operator can be used. Any other operator gives error.
Q12. What will be output for this ?
System.out.print("WelcomeHome" - "Home");
Explanation: We cannot use - with text operand.
Q13. What will be output for this ?
System.out.print("Hello" + 3);
Explanation: Operator: + Operands: "Hello" -> text 3 -> number Since, one operand is text, we concatenate both operands.
Q14. What will be output for this ?
System.out.print("Hello" + 3 + 4);
Explanation: Since, both operators are + and have same priority we will evaluate from left to right => "Hello" + 3 + 4 => "Hello3" + 4 => "Hello34"
Q15. What will be output for this ?
System.out.print("Hello" + 10 + "World");
Explanation: => "Hello" + 10 + "World" => "Hello10" + "World" => "Hello10World"
Q16. What will be output for this ?
System.out.print(10 + "Welcome");
Explanation: => 10 + "Welcome" => "10Welcome"
Q17. What will be output for this ?
System.out.print(10 + 20 + "WakeUp" + 3 + 2);
Explanation: => 10 + 20 + "WakeUp" + 3 + 2 => 30 + "WakeUp" + 3 + 2 => "30WakeUp" + 3 + 2 => "30WakeUp3" + 2 => "30WakeUp32"
Q18. What will be output for this ?
System.out.print("HiGuys" * 2);
Explanation: We cannot use * operator with text operand
Q19. What will be output for this ?
System.out.print(10 + 20 + "WakeUp" + 3 * 2);
Explanation:
=> 10 + 20 + "WakeUp" + 3 * 2 => 10 + 20 + "WakeUp" + 6 => 30 + "WakeUp" + 6 => "30WakeUp" + 6 => "30WakeUp6"
Q20. What will be output for this ?
System.out.print(10 + "Hello" * "World" + 3);
Explanation: We cannot use * operator with text operand
Q21. What will be output for this ?
System.out.print(10 + "WelcomeHome" - "Home" + 3);
Explanation: We cannot use - operator with text operand
Q22. Predict the output for the following code:
System.out.println(10 + 20 + "Hello");
Story : Assume we have a container storing water in it And then explain the three factors of container that are : Type, Name and Value
After that take a container to store integers and explain the same three factors.
In programming, containers are known as variables.
Q1. What will be output for this ?
Create a variable of type int and give name as num and assign 34 in it.
Q2. What will be output for this ?
int val = 30;
System.out.println(val);
Explanation: The value of variable val is 30.
Rule : Whenever we use variable name, the value of that variable is used.
Q3. What will be output for this ?
int a = 10;
int b = 20;
System.out.println(a + b);
Q4. What will be output for this ?
int a = 10;
int b = 20;
System.out.println("Sum of Number is " + a + b);
Explanation:
Here, both operators are + and have same priority. Hence, the exppression will be evaluated from left to right. => "Sum of Number is " + a + b => "Sum of Number is " + 10 + 20 => "Sum of Number is 10" + 20 => "Sum of Number is 1020"
Q5. What will be output for this ?
int a = 10;
int b = 20;
System.out.println("Product of Number is " + a * b);
Explanation: Here, * has highest priority. So, a * b will be evaluated first. => "Product of Number is " + a * b => "Product of Number is " + 10 * 20 => "Product of Number is " + 200 => "Product of Number is 200"
Thank you so much for reading. If you found it valuable, consider subscribing for more such content every week. If you have any questions or suggestions, please email me your comments or feel free to improve it.
I'm Rahul, a Indian Sr. Software Engineer (SDE II) and passionate content creator. Sharing my expertise in software development to assist learners.
More about meExplore Other Topics
A data type is an attribute associated with a piece of data that tells a computer system how to interpret its value. Understanding data types ensures that data is collected in the preferred format and the value of each property is as expected.
The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java.