Skip to content

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

  • by

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.

int x = 30 + 50;

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.

 int marks =  65;

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.

package eye.hunts;

public class A {
    protected void message() {
        //Assign value 
        int marks =  65;
        System.out.println(marks);
    }
}

Output: 65

Arithmetic Operators in Java

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

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

Let’s check the example of Java Arithmetic Operator.

package eye.hunts;

public class A {
    protected void message() {
        //Assign value
        int no1 = 5;
        int no2 = 5;

        //Arithmetic Operators
        System.out.println(no1 + no2); // Addition
        System.out.println(no1 - no2); //Subtraction
        System.out.println(no1 * no2); //Multiplication
        System.out.println(no1 / no2); //Division
        System.out.println(no1 % no2); //Remainder
    }
}

Output: 10
0
25
1
0

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.

OperatorDescriptionExample
||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.

package eye.hunts;

public class A {
    protected void message() {
        //Assign value
        Boolean test1 = true;
        boolean test2 = false;

        //Arithmetic Operators
        System.out.println(test1 && test2); // AND
        System.out.println(test1 || test2); // OR
        System.out.println(!(test1 && test2)); // NOT
    }
}

Output: false
true
true

Bitwise Operators and Shift Operators in Java

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

OperatorDescription
~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.

OperatorDescriptionExample
==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.

OperatorMeaning
+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.

package eye.hunts;

public class A {
    protected void message() {
        //Assign value
        int no1 = 5;
        boolean test = true;

        //Arithmetic Operators
        System.out.println(+no1); // +
        System.out.println(-no1); // -
        System.out.println(++no1); // ++
        System.out.println(--no1); // --
        System.out.println(!test); // !
    }
}

Output: 5
-5
6
5
false

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.

public class A {
    protected void message() {
        int a = 7;
        int b = 9;
        String msg = ((a > b)? "A is Big" : "B is Big ");
        System.out.println(msg);
    }
}

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

Your email address will not be published. Required fields are marked *