Operators in Java and its Types

Swatee Chand
Edureka
Published in
6 min readJul 18, 2019

--

Operators are the constructs that can manipulate the values of the operands. Consider the expression 2 + 3 = 5, here 2 and 3 are operands and + is called operator. In this article on operators, the goal is to get you the expertise required to get started and work with operators in Java.

Java supports the following types of operators:

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

Let’s focus on each of these operators one by one.

Arithmetic Operators in Java

Arithmetic Operators are used to performing mathematical operations like addition, subtraction, etc. Assume that A = 10 and B = 20 for the below table.

Consider the below example:

package Edureka;
public class ArithmeticOperators {
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

Assignment Operators in Java

An Assignment Operator is an operator used to assign a new value to a variable. Assume A = 10 and B = 20 for the below table.

Consider the below example:

package Edureka;public class JavaOperators {
public static void main(String[] args) {
int a = 10;
int b=20;
int c;
System.out.println(c = a); // Output =10
System.out.println(b += a);// Output=30
System.out.println(b -= a);// Output=20
System.out.println(b *= a);// Output=200
System.out.println(b /= a);// Output=2
System.out.println(b %= a);// Output=0
System.out.println(b ^= a);// Output=0
}
}

Moving ahead in Java operator's tutorial, let’s see what are comparison operators.

Relational Operators in Java

These operators compare the values on either side of them and decide the relation among them. Assume A = 10 and B = 20.

Consider the below example:

package Edureka;public class JavaOperators {
public static void main(String[] args) {
int a = 10;
int b=20;
System.out.println(a == b); // returns false because 10 is not equal to 20
System.out.println(a != b); // returns true because 10 is not equal to 20
System.out.println(a > b); // returns false
System.out.println(a < b); // returns true
System.out.println(a >= b); // returns false
System.out.println(a <= b); // returns true
}}

Next up, let’s focus on logical operators in Java.

Logical Operators in Java

The following are the Logical operators present in Java:

Consider the below example:

package Edureka;public class JavaOperators {
public static void main(String[] args) {
int a = 10;
System.out.println(a<10 & a<20); //returns false
System.out.println(a<10 || a<20); //returns true
System.out.println(!(a<10 & a<20)); //returns true
}
}

Now let’s see unary operators in Java.

Unary Operator in Java

Unary operators are the one that needs a single operand and are used to increment a value, decrement or negate a value.

Consider the following example:

package Edureka;public class JavaOperators {
public static void main(String[] args) {
int a = 10;
boolean b=true;
System.out.println(a++); //returns 11
System.out.println(++a);
System.out.println(a--);
System.out.println(--a);
System.out.println(!b); // returns false
}}

Moving ahead, let’s understand bitwise operator in Java

Bitwise Operator in Java

Bitwise operations directly manipulate bits. In all computers, numbers are represented with bits, a series of zeros and ones. In fact, pretty much everything in a computer is represented by bits. Assume that A = 10 and B = 20 for the below table.

Consider the example shown below:

package Edureka;public class JavaOperators {
public static void main(String[] args) {
int a = 58; //111010
int b=13; //1101
System.out.println(a&b); //returns 8 = 1000
System.out.println(a|b); //63=111111
System.out.println(a^b); //55=11011
System.out.println(~a); //-59
}
}

Next up, let’s focus on the ternary operator in Java

Ternary Operators in Java

The ternary operator is a conditional operator that decreases the length of code while performing comparisons and conditionals. This method is an alternative for using if-else and nested if-else statements. The order of execution for this operator is from left to right.

Syntax:

(Condition) ? (Statement1) : (Statement2);
  • Condition: It is the expression to be evaluated which returns a boolean value.
  • Statement 1:It is the statement to be executed if the condition results in a true state.
  • Statement 2:It is the statement to be executed if the condition results in a false state.

Consider the below example:

package Edureka;public class JavaOperators {
public static void main(String[] args) {
int a = 20, b = 10, c = 30, res;
res = ((a > b) ? (a > c)? a: c: (b > c)? b: c);
System.out.println("Max of three numbers = "+ res);
}
}

Output — Max of three numbers = 30

Moving ahead to the last java operator, let’s understand Shift operators in Java.

Shift Operators in Java

Shift operators are used to shifting the bits of a number left or right, thereby multiplying or dividing the number. There are three different types of shift operators, namely left shift operator()<<, signed right operator(>>) and unsigned right shift operator(>>>).

Syntax:

numbershift_op number_of_places_to_shift;

Consider the following example:

package Edureka;public class JavaOperators {
public static void main(String[] args) {
int a=58;
System.out.println(a<<2); //232=11101000
System.out.println(a>>2); //returns 14=1110
System.out.println(a>>>2); //returns 14
}
}

With this, we come to an end of this article on the different Java operators. I hope this article was informative to you.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Java.

1. Object Oriented Programming

2. Inheritance in Java

3. Polymorphism in Java

4. Abstraction in Java

5. Java String

6. Java Array

7. Java Collections

8. Java Threads

9. Introduction to Java Servlets

10. Servlet and JSP Tutorial

11. Exception Handling in Java

12. Advanced Java Tutorial

13. Java Interview Questions

14. Java Programs

15. Kotlin vs Java

16. Dependency Injection Using Spring Boot

17. Java Tutorial

18. Top 10 Java frameworks

19. Java Reflection API

20. Top 30 Patterns in Java

21. Core Java Cheat Sheet

22. Socket Programming In Java

23. Java OOP Cheat Sheet

24. Annotations in Java

25. Library Management System Project in Java

26. Trees in Java

27. Machine Learning in Java

28. Top Data Structures & Algorithms in Java

29. Java Developer Skills

30. Top 55 Servlet Interview Questions

31. Top Java Projects

32. Java Strings Cheat Sheet

33. Nested Class in Java

34. Java Collections Interview Questions and Answers

35. How to Handle Deadlock in Java?

36. Top 50 Java Collections Interview Questions You Need to Know

37. What is the concept of String Pool in Java?

38. What is the difference between C, C++, and Java?

39. Palindrome in Java- How to check a number or string?

40. Top MVC Interview Questions and Answers You Need to Know

41. Top 10 Applications of Java Programming Language

42. Deadlock in Java

43. Square and Square Root in Java

44. Typecasting in Java

45. Operators in Java and its Types

46. Destructor in Java

47. Binary Search in Java

48. MVC Architecture in Java

49. Hibernate Interview Questions And Answers

Originally published at https://www.edureka.co on July 18, 2019.

--

--