JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Java Operators

In this page, we'll learn about different types of operators in Java, their syntax and how to use them with the help of examples.


Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, - is an operator used for subtraction, while / is also an operator used for division.

There are many types of Operators in Java:

1. Arithmetic Operators

2. Unary Operators

3. Assignment Operators

4. Relational Operators

5. Bitwise Operators

6. Logical Operators

1) Java Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on variables and data. They act as basic mathematical operations.

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)

Example of Arithmetic Operators

class OperatorsTest{
    public static void main(String args[]){
    int a = 10;
    int b = 4;
        System.out.println("a + b = " + (a + b)); //addition operator
        System.out.println("a - b = " + (a - b)); //subtraction operator
        System.out.println("a * b = " + (a * b)); //Multiplication operator
        System.out.println("a / b = " + (a / b)); //division operator
        System.out.println("a % b = " + (a % b)); //Modulo operator
    }
}

Output:

a + b = 14

a - b = 6

a * b = 40

a / b = 2

a % b = 2

In the above example, we have used +, -, *, / and % operators to compute addition, subtraction, multiplication division and Modulo operations.

Division Operator /

If we use the division operator with two integers, then the resulting quotient will also be an integer. And, if one of the operands is a floating-point number, we will get the result will also be in floating-point.

Modulo Operator %

The modulo operator % computes the remainder. When a = 10 is divided by b = 4, the remainder is 2.

Note:The % operator is mainly used with integers.

2) Java Unary Operators

Unary operators are used with only one operand. For example, -- is a unary operator that decreases the value of a variable by 1. That is, --5 will return 4

There are many types of unary operators are:

Operator Meaning
+ Unary plus: not necessary to use since numbers are positive without using it
- Unary minus: inverts the sign of an expression
-- Decrement operator: decrements value by 1
++ Increment operator: increments value by 1
! Logical complement operator: inverts the value of a boolean

Increment and Decrement Operators

Java also provides increment and decrement operators: ++ and -- respectively. ++ increases the value of the operand by 1, while -- decrease it by 1.

Example of Increment and Decrement Operators

class OperatorsTest{
    public static void main(String args[]){
    int x = 2;
    int y = 6;
    int a, b;
        System.out.println("Original value of x is: " + x);
        a = ++x; //increment operator
        System.out.println("After increment: " + a);
        System.out.println("Original value of y is: " + y);
        b = --y; //decrement operator
        System.out.println("After decrement: " + b);
    }
}

Output:

Original value of x is: 2

After increment: 3

Original value of y is: 6

After decrement: 5

In the above program, we have used the ++ and -- operator as prefixes (++a, --b). We can also use these operators as postfix (a++, b++).

There is a slight difference when these operators are used as prefix versus when they are used as a postfix.

3) Java Assignment Operators

Java assignment operator is one of the most common operator. It is used to assign the value on its right to the operand on its left.

There are following assignment operators available in Java.

Operator Example Equivalent to
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;

Example of Assignment Operators

class OperatorsTest{
    public static void main(String args[]){
    int x = 20;
    int a;
        a = x; //assign value using =
        System.out.println("Value of a: " + a);
        a -= x; //assign value using -=
        System.out.println("Value of a: " + a);
        a *= x; //assign value using *=
        System.out.println("Value of a: " + a);
    }
}

Output:

Value of a: 20

Value of a: 0

Value of a: 0

4) Java Relational Operators

Relational operators are used to check the relationship between two operands.

There are following relational operators available in Java.

Operator Description
== Is Equal To
!= Not Equal To
> Greater Than
< Less Than
>= Greater Than or Equal To
<= Less Than or Equal To

Example of Relational Operators

class OperatorsTest{
    public static void main(String args[]){
    int a = 10;
    int b = 20;
        System.out.println("Value of a is: " + a + "and b is: "+ b);
        System.out.println(a == b); //using == operator
        System.out.println(a != b); //using != operator
        System.out.println(a > b); //using > operator
        System.out.println(a < b); //using < operator
        System.out.println(a >= b); //using >= operator
        System.out.println(a <= b); //using <= operator
    }
}

Output:

false

true

false

true

false

true

Note: Relational operators are used in decision making and loops.

5) Java Bitwise Operators

Bitwise operators in Java are used to perform operations on individual bits.

There are following bitwise operators available in Java.

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

Example of Bitwise Operators

6) Java Logical Operators

Logical operators are used to check whether an expression is true or false. They are used in decision making.

There are following logical operators available in Java.

Operator Description
|| Logical OR
&& Logical AND
! Logical NOT

Example of Logical Operators

class OperatorsTest{
    public static void main(String args[]){
    int a = 10;
    int b = 6;
    int c = 23;
        System.out.println((a > b) || (c < a); //using || operator
        System.out.println((a < b) || (c > a);
        System.out.println((a < b) || (c < a);

        System.out.println((a > b) && (c > a); //using && operator
        System.out.println((a > b) && (c < a);

        System.out.println(a == b); //using ! operator
        System.out.println(a > b);
    }
}

Output:

true

true

false

true

false

true

false