As the name gives out, operators in Java perform various operations on different variables and values. Java provides many types of operators to perform other actions. In this tutorial, you will go through all kinds of operators with examples.

Different Types of Operators in Java

Java offers the following operators:

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

Now that you know the types of operators in Java, it’s time to understand each of them with examples.

Java Unary Operators

Unary operators in Java require only one operand. They are used to increment/decrement and negotiate a value. It is also possible to invert a Boolean value with unary operators. The table depicted below shows different unary operators and what they do.

Unary Operator

Description

++

It increments a value by 1. If used before the expression, the value is first incremented, and then the result is calculated. When used after the expression, the result is first calculated, and then the value is incremented.

--

It decrements a value by 1. If used before the expression, the value is first decremented, and then the result is calculated. When used after the expression, the result is first calculated, and then the value is decremented.

~

It is used to negotiate a value.

!

It inverts a Boolean value.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

Here’s an example using the unary operators.

unaryoperators

unaryoperators_output.

Java Arithmetic Operators

Arithmetic operators in Java perform multiplicative and additive operations such as addition, subtraction, multiplication, division, and modulus. The table below shows different arithmetic operators and what they do.

Arithmetic Operator

Description

+

It performs addition

-

Used for subtraction

*

Performs multiplication

/

It is used for division

%

It performs modulo (returns the remainder).

The example below shows the usage of all the arithmetic operators.

arithmeticoperators

arithmeticoperators_ot

Java Bitwise Operators

Bitwise operators in Java perform operations on individual bits. The table below shows different bitwise operators and what they do.

Bitwise Operator

Description

&

The bitwise AND operator will return the bit-by-bit AND result of each input value

|

The bitwise OR operator will return the bit-by-bit OR result of each input value

^

The bitwise XOR operator will return the bit-by-bit XOR result of each input value

~

The bitwise complement operator will inverse the bits of all the input values

Let’s look at the example below to understand the bitwise operator. Consider the value of a is 108, and that of b is 29. The binary format of these values is:

108: 0110 1100

29: 0001 1101

Now the values of:

a&b = 0000 1100 = 12

a|b = 0111 1101 = 125

a^b = 0111 0001 = 113

~a = 1001 0011 = -109 (Don’t get confused, it calculates the answer in 2’s complement form as it is a signed binary number).

Now, look at whether our Java program gives the same results.

BitwiseOperators

BitwiseOperators_ot

Java Logical Operators

Logical operators in Java are used to perform logical AND, OR, and NOT operations. The table below shows different logical operators and what they do.

Logical Operators

Description

&&

The logical AND returns true, if all the expressions are true

||

The logical OR returns true, if either of the expressions is true

!

The logical NOT returns the inverse of the expression (if true then false and vice versa)

The example below shows the use of logical operators in Java.

LogicalOperators

LogicalOperators_ot

The significant difference between the logical AND and bitwise AND is that the logical AND only checks the second expression in the event that the first one is true. Whereas, the bitwise AND operator will check the second expression even if the first one is true. Similarly, the difference between logical OR and bitwise OR is that the logical OR would check the 2nd expression, only in the case where the first one is false. On the other hand, the bitwise OR will check the second expression even if the first one is false.

Java Relational Operators

Relational operators in Java are used to check relations between two operands. The table below shows different relational operators and what they do.

Relational Operators

Description

==

Returns true if the first expression is equal to the second expression

!=

Returns true if the left operand is not equal to the right one

<

Returns true if the left operand is less than the right one

<=

Returns true if the left operand is either less than or equal to the right operand

>

Returns true if the left operand is greater than the right one

>=

Returns true if the left operand is either greater than or equal to the right operand

instanceof

Returns true if the first operand (usually object) is an instance of the second operand (usually a parent class)

Let’s look at an example to see the use of relational operators in Java.

RelationalOperators

RelationalOperators_ot

Java Shift Operators

Shift operators in Java are used to shift the binary bits of a number to the left or right. By shifting the bits, it will multiply or divide the operand by two. The table below shows different shift operators and what they do.

Shift Operators

Description

<<

The left shift operator shifts the bits to the left by the number mentioned, thereby multiplying the number by 2 to the specified number’s power

>>

The right shift operator shifts the bits to the right by the number mentioned, thereby dividing the number by 2 to the specified number’s power

>>>

The unsigned right shift operator shifts the bits to the right and changes the parity bit to 0, but only if the input value is negative

The example below shows the use of shift operators.

ShiftOperators

ShiftOperators_ot

Java Ternary Operator

A ternary operator in Java is used as a shorthand replacer to if-else statements. It is also referred to as a miscellaneous operator. The table below shows different ternary operators and what they do.

Ternary Operator

Description

? :

If the condition is true, the statement before the ‘:’ is executed; otherwise, the statement after the ‘:’ is executed

Let’s look at an example to use the ternary operator in Java.

TernaryOperator

TernaryOperator_ot

Become a Certified UI UX Expert in Just 5 Months!

With The Best-in-class UI UX ProgramExplore Program
Become a Certified UI UX Expert in Just 5 Months!

Java Assignment Operators

As the name suggests, assignment operators in Java assign values specified on its right, to the operands on its left. It assigns the right value to the left operand because it has a right to left associativity (you will look at precedence and associativity of each type of operator later in this tutorial). The table below shows different assignment operators and what they do.

Assignment Operator

Description

=

It assigns the value of operand on its right, to the one on its left

You can also combine the assignment operator with other operators to shorten the statement. For instance, we can combine = with + to write a+=10, instead of writing a=a+10. Similarly, we can combine = with -, *, /, %, and more operators. The example below shows the use of the assignment operator.

AssignmentOperators

AssignmentOperators_ot

Java Operators Precedence and Association

When there is over one operator in a single statement, precedence determines how they will be evaluated and executed. For instance, multiplication has higher precedence than addition. Hence, in the statement a=8+3*4, a will become 20 and not 44 as 3 will be multiplied by 4 first and then added to 12. Similarly, all the operators in Java have precedence. They also have an association, which determines whether the operators will be evaluated from left to right or vice versa. The table below shows Java operators’ precedence and association. Operators higher in the table have higher precedence than the lower ones.

Operator Type

Category

Precedence

Association

Unary

Postfix

expr++  expr--

Left to right

Prefix

++expr  --expr  +expr  -expr  ~  !

Right to left

Arithmetic

Multiplicative

*  /  %

Left to right

Additive

+  -

Left to right

Shift

Shift

<<  >>  >>>

Left to right

Relational

Comparison

<  >  <=  >=  instanceof

Left to right

Equality

==  !=

Left to right

Bitwise

AND

&

Left to right

XOR

^

Left to right

OR

|

Left to right

Logical

AND

&&

Left to right

OR

||

Left to right

Ternary

Ternary

? :

Right to left

Assignment

Assignment

=  +=  -=  *=  /=  %=  &=  ^=  |=  <<==  >>==  >>>==

Right to left

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

If you are interested in Java programming and want to pursue a software development career, Simplilearn’s Online Java Certification Course can help you excel. The course offers 60 hours of applied learning and implementation projects to help you grasp Java programming concepts.

If you have any questions for us, leave them in the comments section of this article. Our experts will get back to you on the same, ASAP!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449