TeachingBee

Different Types Of Operators In Java

types of operators in java

What are Operators in Java?

Operators Definition In Java: Operators in Java are special symbols that perform specific operations on one or more operands (variables or values). Understanding different types of operators in java is critical for any Java developer as they allow you to manipulate variables and values in your code.

Some key points about operators in Java:

  • Operators allow you to perform operations like arithmetic, comparison, logical, bitwise etc. on variables and values.
  • They usually appear in between the operands they act on. For example: a + b (performs addition on a and b).
  • Java has a rich set of inbuilt operators that are grouped into categories like: arithmetic, relational, logical, unary etc.
  • You can use operators to assign values, check conditions, perform computations and manipulate bits in Java.
types of operators in java
Types of Operators in Java

Different Types Of Operators In Java

These are the primary types of operators in Java programming.

  1. Arithmetic Operators ( +, -, *, /, % )
  2. Relational Operators (Comparison Operators) ( ==, !=, >, < )
  3. Logical Operators ( &&, ||, ! )
  4. Bitwise Operators ( &, |, ~ )
  5. Unary Operators ( -, +, ++, — )
  6. Assignment Operators ( =, +=, -= etc. )
  7. Ternary Operator (Conditional Operator) ( ? : )
  8. Shift Operators ( <<, >> )

Now that we know what operators are in Java, let’s discuss the various types of operators in Java with example in detail:

Types Of Operators In Java With Example Code

Arithmetic Operators In Java

Arithmetic operators are used to perform common mathematical operations on numeric operands.

Some key arithmetic operators in Java are:

  • Addition (+) : Adds two values
// Types Of Operators In Java: Arithmetic Operators In Java

int x = 5;
int y = 10;
int sum = x + y; //sum = 15
  • Subtraction (-) : Subtracts second value from the first
// Types Of Operators In Java: Arithmetic Operators In Java

int m = 15;
int n = 5;
int diff = m - n; //diff = 10
  • Multiplication (*) : Multiplies two values
// Types Of Operators In Java: Arithmetic Operators In Java

int a = 5;
int b = 2;
int product = a * b; //product = 10
  • Division (/) : Divides first value by second
// Types Of Operators In Java: Arithmetic Operators In Java

int p = 20;
int q = 4;
int quotient = p / q; //quotient = 5
  • Modulus (%) : Gives remainder on division
// Types Of Operators In Java: Arithmetic Operators In Java

int r = 13;
int s = 5;
int remainder = r % s; //remainder = 3

These arithmetic operators can be used to perform basic to complex mathematical operations in Java code.

Relational Operators In Java

Relational operators are used to compare operands and check for relationships between them. They return a boolean value based on the condition.

Some key relational operators are:

  • Equal to (==) : Checks if operands are equal
// Relational Operators In Java

int a = 5;
int b = 5;
boolean res = (a == b); // true
  • Not equal to (!=) : Checks if operands are not equal
// Relational Operators In Java

String s1 = "Hello";
String s2 = "World";
boolean res = (s1 != s2); // true
  • Greater than (>) : Checks if first value is greater than second
// Relational Operators In Java

float n1 = 6.5f;
float n2 = 3.5f;
boolean res = (n1 > n2); // true
  • Less than (<) : Checks if first value is less than second
// Relational Operators In Java

double d1 = 15.0;
double d2 = 20.0;
boolean res = (d1 < d2); // true
  • Greater than or equal to (>=) : Checks if first value is greater than or equal to second
// Relational Operators In Java

int num1 = 10;
int num2 = 10;
boolean res = (num1 >= num2); // true
  • Less than or equal to (<=) : Checks if first value is less than or equal to second
// Relational Operators In Java

long l1 = 15L;

long l2 = 15L;

boolean res = (l1 <= l2); // true

These relational operators is one of the types of operators in java allow you to compare values and variables in Java. They are heavily used in control statements and loops for decision making.

Logical Operators in Java

Logical operators are used to make logical decisions by evaluating multiple boolean expressions.

Some common logical operators are:

  • Logical AND (&&) : Evaluates to true only if both conditions are true. It Doesn’t check second condition if first is false
// Relational Operators In Java

int x = 5;
boolean res = (x > 0 && x < 10); // true
  • Logical OR (||): Evaluates to true if any one condition is true. Doesn’t check second condition if first is true
// Relational Operators In Java

int n = -3;
boolean res = (n < 0 || n > 0); // true

These logical operators allow you to combine multiple conditions and achieve complex logic in Java.

Understanding Unary and Bitwise Operators

Unary operators require only one operand. Bitwise operators perform computations at bit level.

Some examples:

  • Logical NOT (!) : Inverts or flips the boolean value
// Relational Operators In Java

boolean success = false;
boolean res = !success; // true
  • Unary minus (-) : Negates an expression
// Unary and Bitwise Operators

int x = 5;
int res = -x; // res = -5
  • Unary plus (+) : Returns value unchanged
// Unary and Bitwise Operators 

float num = 5.5f;
float res = +num; // res = 5.5
  • Increment (++) : Increases value by 1 (prefix and postfix)
// Understanding Unary and Bitwise Operators

int a = 5;
int res = ++a; // res = 6, a = 6

int b = 5;
int res = b++; // res = 5, b = 6
  • Decrement (--) : Decreases value by 1 (prefix and postfix)
int x = 5;
int res = --x; // res = 4, x = 4

int y = 5;
int res = y--; // res = 5, y = 4
  • Bitwise AND (&) : Bitwise AND of values
int a = 5; // 0101
int b = 3; // 0011
int res = a & b; // 0001 = 1
  • Bitwise OR (|) : Bitwise OR of values
int c = 5; // 0101
int d = 3; // 0011
int res = c | d; // 0111 = 7
  • Bitwise NOT (~): Inverts all bits of value
int n = 5; // 0101
int res = ~n; // 1010 = -6
  • Bitwise XOR (^): Bitwise XOR of values
int c = 5; // 0101
int d = 3; // 0011
int res = c ^ d; // 0110 = 6

These unary and bitwise operators give you more fine-grained control in Java.

Shift Operators in Java

Shift operators move the bits of a value left or right by specified number of positions.

The two shift operators in Java are:

  • Left shift (<<) : Shifts bits to the left and fills with 0 on right
int a = 5; // 0101
int res = a << 1; // 1010 = 10
  • Right shift (>>) : Shifts bits to the right and fills with 0 on left
int b = 10; // 1010
int res = b >> 1; // 0101 = 5
  • Unsigned Right Shift (>>>) : It shifts the bits of a value to the right by the specified number of positions and fills the vacant left positions with zeros.

Here is an example to demonstrate the >>> operator:

int x = 16; // 00010000 in binary 
int y = x >> 2;   // y= 4

int z = x >>> 2;  // z=4

int a = -16; // 11100000 in binary, first bit is sign bit
int b = a >> 2;  // b=-4
 

Ternary Operator in Java

The ternary operator allows concise conditional logic in Java.

It has the following syntax:

condition ? expr1 : expr2

If condition is true, expr1 is evaluated, else expr2 is evaluated.

For example:

// Ternary Operator in Java

int age = 20;
String res = (age >= 18) ? "Eligible" : "Not Eligible";
// Sets res to "Eligible"

The ternary operator allows compact if-else logic in Java.

Assignment Operators and Their Shortcuts

Assignment operators allow you to assign values to variables in shortcuts.

Some examples are:

  • Simple Assignment (=) : Assigns value on right to variable on left
int a = 5;
// Assigns 5 to variable a
  • Add and Assign (+=) : Adds right value to variable
int num = 5;
num += 2; // num = num + 2 = 7 
  • Subtract and Assign (-=) : Subtracts right value from variable
int n = 10;
n -= 5; // n = n - 5 = 5
  • Multiply and Assign (*=) : Multiplies variable with value
int x = 5;
x *= 2; // x = x * 2 = 10
  • Divide and Assign (/=) : Divides variable by value
int y = 20;
y /= 5; // y = y / 5 = 4

These shorthand assignment operators allow you to simplify code and do operations in-place.

Summary

Operator TypeOperatorsDescriptionExample
Arithmetic+, -, *, /, %Performs arithmetic operationsx + y, x – y, x * y, x / y, x % y
Relational==, !=, >, <, >=, <=Checks relational conditionsx == y, x != y, x > y, x < y, x >= y, x <= y
Logical&&, ||Evaluates logical conditionsx > 0 && y > 10, x == 0 || y == 0
Unary-, +, ++, –, !Operates on single operand-x, +x, ++x, –x, , !(x > y)
Bitwise&, |, ~, ^Performs bitwise operationsx & y, x | y, ~x, x ^ y
Shift<<, >>, >>>Shifts bits left or rightx << 2, x >> 2, x >>> 2
Ternary? :Conditional expressionx > 0 ? 1 : 0
Assignment= += -= *= /= %= &= ^= |= <<= >>= >>>=Assigns valuesx = 5, x += 2, x -= 5, x *= 2, x /= 4, x %= 3
Different types of operators in Java With Example

Conclusion and Best Practices

We have covered all the important different types of operators in Java, along with usage examples. Here are some key tips to use them effectively:

  • Know operator precedence to avoid issues and bugs
  • Use relational and logical operators for robust conditions and flow control
  • Prefer prefix increment/decrement over postfix if possible
  • Use bitwise operators only if required for performance reasons
  • Leverage ternary operator to write concise conditional logic
  • Use compound assignment operators to simplify code

Some common mistakes to avoid

  • Confusing = and == operators in conditions
  • Forgetting semicolons after statements
  • Using bitwise operators unnecessarily
  • Overloading code with too many operators

Mastering operators allows you to write optimised Java code. Use them judiciously to develop clean and efficient programs.

Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.

FAQ Related To Types Of Operators In Java

Which operator has the highest precedence in Java?

A: The postfix increment and decrement operators (x++, x–) have the highest precedence in Java. They are followed by unary (+, -, ++x, –x), multiplicative (*, /, %), additive (+, -) and so on.

What is the syntax of the ternary operator?

A: The syntax of the ternary operator is:

(condition) ? expression1 : expression2

If condition is true, expression1 is evaluated, else expression2 is evaluated.

How do bitwise operators work in Java?

A: Bitwise operators work at the bit level in Java. Bitwise AND (&), OR (|) and NOT (~) perform boolean logic on the individual bits of the operands. Bitwise shift operators (<<, >>) shift the bits of the first operand left or right by the number of positions specified in the second operand.

When should I use assignment operators vs normal assignment?

A: Use compound assignment operators like +=, -= when you want to update the variable’s value in-place. Use simple = assignment when you just want to assign a completely new value to a variable. Compound assignments improve code conciseness.

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

ascii value in java

Print ASCII Value in Java

In this article we will into the ASCII table in C, exploring its various character sets—from control characters to printable characters, including letters, digits, and special symbols—and demonstrates how to

data hiding in java

Data Hiding In Java

In this article we will discuss data hiding in Java which is an important concept in object-oriented programming. We will cover So let’s get started. What is Data Hiding in

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In