Java Operators | Ternary, Bitwise, Arithmetic, Assignment, Logical

Operators are a very important role in any programming language, it performs logic and other important operation. Java Operators have performed an operation in the program and application like addition, division, etc.

Where Operators in an easy word is a symbol/characters ( +, -, *, / etc.) used between variables (operands) and conditions.

Java Operators | Ternary, Bitwise, Arithmetic, Assignment, Logical examples

This tutorial you will learn about All types of Java operators with syntax and the latest examples.

Java Operator Example

Here is a simple example of adding 2 operands in java.

Types of Operators in Java

Here are the types of operators in java below:

  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Misc Operators
  • Unary Operator
  • Ternary Operator

Operator Precedence

The grouping of terms in an expression that affects how an expression is evaluated in Java Operator Precedence.

Operator TypeCategoryPrecedence
Unarypostfixexpr++ expr--
prefix++expr --expr +expr -expr ~ !
Arithmeticmultiplicative* / %
additive+ -
Shiftshift<< >> >>>
Relationalcomparison< > <= >= instanceof
equality== !=
Bitwisebitwise AND&
bitwise exclusive OR^
bitwise inclusive OR|
Logicallogical AND&&
logical OR||
Ternaryternary? :
Assignmentassignment=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=

Assignment Operators in Java

Let’s start with a basic one, how to assign the value to variables?

It has done by using Assignment Operators, let’s see an example and code below. In Code where int is a Data type, marks are variables name and 65 is a variable value.

The Java assignment operators assign ( = )  the value on its right to the variable on its left.

A complete example of Assignment Operators, with print the value in the console.

Output: 65

Arithmetic Operators in Java

To perform a math (Methamthical) operation in program or application in java, you have to Arithmetic Operator.

+Addition (also used for string concatenation)
Subtraction Operator
*Multiplication Operator
/Division Operator
%Remainder Operator

Let’s check the example of Java Arithmetic Operator.

Output: 10

Logical Operators in JAVA

Java Logical operators are used to get the logic between variables or values for an operation. Here’s how they work.

||conditional-OR; true if either of the boolean expression is truefalse || true is evaluated to true
&&conditional-AND; true if all boolean expressions are truefalse && true is evaluated to false

Java Operator OR & And Examples.

Output: false

Bitwise Operators and Shift Operators in Java

To perform bit shift operators in Java, Bitwise operators are used.

~Bitwise Complement
<<Left Shift
>>Right Shift
>>>Unsigned Right Shift
&Bitwise AND
^Bitwise exclusive OR
|Bitwise inclusive OR

Relational Operators in Java

To Find a Relationship between two operands uses Relational Operator like operands are equal, greater, etc. And the result is in Boolean: true or false.

==equal to4 == 7 is evaluated to false
!=not equal to4 != 7 is evaluated to true
>greater than4 > 7 is evaluated to false
<less than4 < 7 is evaluated to true
>=greater than or equal to4 >= 4 is evaluated to true
<=less then or equal to8 <= 8 is evaluated to true

A Unary operator in java

The unary operator performs an operation on only one operand. Mostly using increment or decrement of variables value in Loop statement and if-else block.

+Unary plus (not necessary to use since numbers are positive without using it)
Unary minus; inverts the sign of an expression
++Increment operator; increments value by 1
decrement operator; decrements value by 1
!Logical complement operator; inverts the value of a boolean

Java Unary operator example and use.

Output: 5

Ternary Operator in Java

Java Ternary operator is mostly used for replacement for the if-else statement. It a linear statement, and used only a conditional operator which takes three operands.

Here is a Java Ternary Operator Example.

Output: B is Big

This is a Tutorial on Java and operators (how its work get to gather) with many examples, Do share your doubt suggestions in the comment section.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java operators Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.