×
☰ See All Chapters

Operators in Java

An operator is something that performs some action on one or more values and yields another value. Variables which hold the value are called operands. Operator together with operands forms an expression. Below table lists out all the operators those can be used in java.

Operator category

Operators

Precedence

Associativity

Parentheses, braces

(  ), { }, [  ]

1

L to R

Unary operators

++ , -- ,  + ,  - ,  ~ ,  !

2

R to L

Multiplicative

*, / , %

3

L to R

Additive

+ , -

4

L to R

Shift

<<, >>, >>>

5

L to R

Relational

<, >, <=, >=, instanceof

6

L to R

Equality

==, !=

7

L to R

Bitwise

&, ^, |

8

L to R

Logical

&&, ||

9

L to R

Conditional

? :

10

R to L

Assignment

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

11

L to R

Parentheses, braces

In any expression whatever the thing present inside the Parentheses/braces must be evaluated first.

Increment and Decrement Operators

The increment operator ++ adds one to a number, and the decrement operator – – subtracts one from a variable. The two operators are applied as either a prefix or a suffix to any variable that represents a number. Using these operators as a prefix is referred to as pre increment. Using these operators as a suffix to a variable is referred to post increment.

++var

Increment and use. Increment the variable by 1 before it is used

var++

Use and Increment. Use the value first and then increment the value by 1

--var

Decrement and use. Decrement the variable by 1 before it is used

var--

Use and Decrement. Use the value first and then Decrement the value by 1

 

package com.java4coding;

 

public class HelloWorld {

        public static void main(String[] args) {

                int x = 5;

                System.out.println(x++);// 5 (6)

                System.out.println(++x);// 7

                System.out.println(x--);// 7 (6)

                System.out.println(--x);// 5

        }

}

Output

5

7

7

5

Arithmetic Unary operators

Description

Symbol

Example

result

Unary plus

+

+6

6

Unary minus

-

-6

-6

 

Logical Not Operators

The logical not denoted by ! can be true or false (Boolean data Type) .

Operand

!(operand)

true

false

false

true

 

Bitwise Negate Operators

This operator gives the one’s complement of the operand value i.e., if a bit position is 0 then it is changed to 1 and  if bit position is 1 then it is changed to 0.

 

class Demo {

        public static void main(string args[]) {

                int a = 15, b;

                b = ~a;

                System.out.println(a);// Prints -16

        }

}

 

Multiplicative Operators

Assume variable M holds 10 and variable N holds 20, then:

 

Operator

Description

Example

*

Multiplies both operands

M*N will give 200

/

Divides numerator by de-numerator

M/N will give 2

%

Remainder after an integer division

M%N will give 0

 

Additive Operators

Assume variable M holds 10 and variable N holds 20, then:

Operator

Description

Example

+

Add

M+N will give 30

-

Subtract

M-M will give -10

 

Shift Operators

Operator

Description

<<

Perform binary left shift. 0 to be placed in the least-significant digit, shifting all the other 1s and 0s one place to the right, and having the most significant digit “pushed” off the end and lost. This is equivalent to multiplying by two.

>>

Perform signed binary right shift operation. If MSB is 1, then fill 1 to shifted MSB. If MSM is 0, then fill 0 to shifted MSB. Hence bring in the sign bit on right. This is equivalent to dividing by two.

>>>

Perform unsigned binary right shift operation. Always fill 0 to shifted position.

This operation produces no meaningful mathematical result.

 

public class ShiftDemo {

        public static void main(String[] args) {

                byte b = 11;

                System.out.println(b << 1); // Shift to the left

                System.out.println(b >> 1); // Signed shift to the right

                System.out.println(b >>> 1); // Unsigned shift to the right

                byte c = -10;

                System.out.println(c << 3); // Shift to the left three

                System.out.println(c >> 1); // Sign shift to the right

                System.out.println(c >>> 1);

        }

}

 

Relational Operators

The relational operators determine the relationship that one operand has to the other. The outcome of these operations is a boolean value. Only numeric types can be compared using the ordering operators. That is, only integer, floating-point, and character operands may be compared to see which is greater or less than the other.

int done;

// ...

if(!done) ... // Valid in C/C++

if(done) ... // but not in Java.

In Java, these statements must be written like this:

if(done == 0) ... // This is Java-style.

if(done != 0) ...

 

public class ShiftDemo {

        public static void main(String[] args) {

                int a = 4;

                int b = 1;

                boolean c = a < b;

                System.out.println(c);

        }

}

   In this case, the result of a<b (which is false) is stored in c.

Equality Operators

Equality operators are also relational operators. The outcome of these operations is a boolean value. Assume variable M holds 10 and variable N holds 20, then:

Operator

Description

Example

==

Checks if the values of two operands are equal or not, if yes then condition becomes true.

(M== N ) is not true

!=

Checks if the values of two operands are equal or not, if not equal then condition becomes true.

(M != N ) is not true

 

Bitwise Operators

Bitwise operator works on bits and performs bit-by-bit operation. The truth tables for &, |, and ^ are as follows:

a

b

a&b

a|b

a^b

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

1

1

 

Assume if A = 60; and B = 13; now in binary format they will be as follows:

A = 0011 1100

B = 0000 1101

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

class BitwiseDemo {

        public static void main(String[] args) {

                System.out.println("Hello World!");

                byte a, b, c;

                int d;

                a = 60;

                b = 13;

                System.out.println("a=" + a);

                System.out.println("b=" + b);

                System.out.println("a&b=" + (a & b));

                System.out.println("a|b=" + (a | b));

                System.out.println("a^b=" + (a ^ b));

                // c = a|b; error: possible loss of precision

                d = a | b;

                System.out.println(d);

        }

}

Output:

Hello World!

a=60

b=13

a&b=12

a|b=61

a^b=49

61

Logical Operators

Following table shows all the logical operators. Assume variable M holds true and variable N holds false, then:

Operator

Description

Example

&&

If both the operands are true then condition becomes true.

(M&&N) will give false

||

If any one of the operands is true then condition becomes true.

(M||N) will give true

!

Use to revere the logical state of its operand. If condition is true, then logical not operator makes it false.

!(M&&N) will give true

 

Conditional Operators

It is called the ternary operator because it has three operands, and it is basically a shortcut mechanism for writing an if/else control structure. The syntax is the following:

(boolean expression) ? x : y

If the Boolean expression is true, the x statement is executed. If the Boolean expression is false, the y statement is executed.

Assignment Operators

It has this general form:

variable = expression;

Here, the type of variable must be compatible with the type of expression.

There are following assignment operators supported by java language:

Operator

Description

Example

=

Simple assignment, Assigns values from right side operands to the left side operand

K = M+N will assign value f  M+N to K

+=

Add AND assignment. It adds right operand to the left operand and assigns the result to left operand.

M+=N is equivalent to M = M+N

-=

Subtract and assignment. It subtracts right operand from the left operand and assigns the result to left operand.

M-=N is equivalent to M = M-N

*=

Multiply AND assignment. It multiplies right operand to the left operand and assigns the result to left operand.

M*=N is equivalent to M = M*N

/=

Divide AND assignment. It divides right operand from the left operand and assigns the result to left operand.

M/=N is equivalent to M = M/N

%=

Modulus AND assignment. It takes modulus of two operands and assigns result to the left operand.

M%=N is equivalent to M = M%N

<<=

Left shift AND assignment

M<<=N is equivalent to M = M<<N

>>=

Right shift AND assignment

M>>=N is equivalent to M = M>>N

&=

Bitwise AND assignment

M&=N is equivalent to M = M&N

^=

Bitwise exclusive OR  and assignment

M^=N is equivalent to M = M^N

|=

Bitwise inclusive OR  and assignment

M|=N is equivalent to M = M|N

 

 

 

 

 

 


All Chapters
Author