Blog

Operators in Java

  • (4.0)
  • | 697 Ratings

Java has a rich set of operators. Operators in Java are a symbol which is used to perform some operations on data. They can be divided into below categories.

Arithmetic Operators

Arithmetic Operators do the same operations which they do in algebra for any mathematical expressions. Below is the table showing arithmetic operator with its meaning.

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus – returns the reminder of a division
++ Increment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
-- Decrement
public class opTest{
 
     public static void main(String args[]) {
 
        System.out.println("Arithmetic Operators");
        int x = 2 + 3;
        int y = x * 2;
        int z = y / 2;
        int w = y - z;
        int m = -w; //in this case - operator will work us unary minus operator
        int n = 22 % 10;
        System.out.println("x = " + x);
        System.out.println("y = " + y);
        System.out.println("z = " + z);
        System.out.println("w = " + w);
        System.out.println("m = " + m);
        System.out.println("n = " + n);
        }
}

Now let us use the basic arithmetic operators in our first example for better insight. They all behave same way they behave in algebra.

When we run this program, below output we get:

Output:

Arithmetic Operators
x = 5
y = 10
z = 5
w = 5
m = -5
n=2

Arithmetic Assignment Operators

In Java, we have a special way to use arithmetic operation with assignment. These are called compound assignment operators. Below is the table showing how we can use them in code.

For example,

Expression Equivalent Expression using compound assignment operators
a = a+4 a+= 4
b = b-5 b-= 5
c = c*8  c*= 8
d = d/3  d/= 3
e = e%5  e%=5
class opTest {
public static void main(String args[]) {
 
System.out.println("Arithmetic Compound Operators");
int x = 1;
        int y = 2;
        int z = 3;
        int w ;
        x += 5;
        y *= 4;
        z += x * y; // add z value into multiplication of x and y
        w = z;
        w %= 6; 
        System.out.println("x = " + x);
        System.out.println("y = " + y);
        System.out.println("z = " + z);
        System.out.println("w = " + w);
        }
}

They are very useful as it saves a bit of typing and they are better implemented by Java than their long forms.  Here is an example of showing their use. 

Output:

Arithmetic Compound Operators
x = 6
y = 8
z = 51
w = 3

Check Out Core Java Tutorials

Increment and Decrement Operators

Java’s increment and decrement operators are ++ and -- accordingly. Increment operator increases operand’s value by 1 whereas decrement operator decreases operand’s value by 1.

Expression Equivalent Expression using increment and decrement operators
a = a + 1 a++ or ++a
b = b - 1 b-- or --b

These operators can appear in postfix or prefix form. These prefix and postfix form perform the same operation which is incrementing or decrementing the operand by 1. But they make a lot of difference when they are used in expressions. When prefix operator is used, the value will be incremented/ decremented by 1 on the first note and then assigned and utilized further in expression. On the other hand if postfix operator is used, first the value will be assigned and then it is incremented/ decremented by 1. Let us look at the example to understand more.

class opTest {
public static void main(String args[]) {
        System.out.println("Arithmetic Increment/Decrement Operators");
        int x = 2;
        int y = 1;
        int m;
        int n;
        m = ++x; //x will be incremented first and then assigned to m
        n = y++; // y will be assigned to n first and then incremented by 1
        System.out.println("x = " + x);
        System.out.println("y = " + y);
        System.out.println("m = " + m);
        System.out.println("n = " + n);
        }
}

Output:

Arithmetic Increment/Decrement Operators
x = 3
y = 2
m = 3
n = 1

Bitwise Operators

Java has several bitwise operators which are only applicable to integer types - long, int, short, char, and byte. Here are the bitwise operators available in Java:

Operator Result
Bitwise unary NOT
& Bitwise AND
Bitwise OR
^ Bitwise exclusive OR
>>  Shift right
>>> Shift right zero fill
<<  Shift left
&= Bitwise AND assignment
|=  Bitwise OR assignment
^=  Bitwise exclusive OR assignment
>>=  Shift right assignment
>>>=  Shift right zero fill assignment
<<=  Shift left assignment

Bitwise operators manipulate bits within an integer.  All integers are stored as binary numbers in Java. So byte value of 42 in binary is 00101010. Here each position refers a power of 2.

Below is the table showing result of each bitwise operator.

X Y X|Y X&Y X^Y !X
0 0 0 0 0 1
1 0 1 0 1 0
0 1 1 0 1 1
1 1 1 1 0 0

Let us understand it by below example.

class opTest {
public static void main(String args[]) {
        int x = 3; // 0 + 2 + 1 or 0011 in binary
        int y = 2; // 4 + 2 + 0 or 0110 in binary
        int a = x | y;
        int b = x & y;
        int c = x ^ y;
        int d = ~x ;
        System.out.println(" x = " + x);
        System.out.println(" y = " + y);
        System.out.println(" x|y = " + a);
        System.out.println(" x&y = " + b);
        System.out.println(" x^y = " + c);
        System.out.println(" !x = " + d);
 
        }
}

Output:

 x = 3
 y = 2
 x|y = 3
 x&y = 2
 x^y = 1
 !x = -4

Now let us understand how this result is derived:

Bitwise OR 

x =3<span style="white-space: pre;"> </span>0011
y=2<span style="white-space: pre;"> </span>0010
x|y <span style="white-space: pre;"> </span>0011 (which is 3)

Bitwise AND

x =3<span style="white-space: pre;"> </span>0011
y=2<span style="white-space: pre;"> </span>0010
x&y<span style="white-space: pre;"> </span>0010 (which is 2)

Bitwise XOR

x =3<span style="white-space: pre;"> </span>0011
y=2<span style="white-space: pre;"> </span>0010
x^y<span style="white-space: pre;"> </span>0001 (which is 1)

Bitwise NOT

x =3<span style="white-space: pre;"> </span>0011
!x<span style="white-space: pre;"> </span>1100 (which is -4)

Shift Operators

Left and Right shift operators shift the value to left or right for the specified number of times respectively. Its syntax is like shown below:

value << num 
value>> num
Below is an example to understand it in a better way:
class opTest {
public static void main(String args[]) {
        int x = 4 ;
        int i, j;
        i = x << 1;
        j = x >> 1;
        System.out.println("Original value of x: " + x);
        System.out.println("i and j: " + i + " " + j);
        }
}

Output:

Original value of x: 4
i and j: 8 2

x=4<span style="white-space:pre"> </span>0100
x<<1<span style="white-space:pre"> </span>1000 (shifted by 1 bit to left – which result into binary value of 8)
x>>1<span style="white-space:pre"> </span>0010 (shifted by 1 bit to right – which result into binary value of 2)

Check Out Core Java interview questions

Relational Operators

Relational operators check the relationship between operands. Relational operators in Java are:

Operator  Result
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Relational operators always return a Boolean value which is true or false. These statements are most frequently used in control statements like conditional statements and loops. Here is the example showing the use of relational operators.

class opTest {
public static void main(String args[]) {
        System.out.println("Relational Operators");
        int a = 1;
        int b = 6;
        boolean c = a < b;
        System.out.println(a == b);
        System.out.println(c);
        }
}

Output:

Relational Operators
false
true

Boolean Logical Operators

Boolean logical operators operate on boolean operands only. They will perform operation on boolean operands and result into a boolean value. Here are the boolean logical operators in Java:

Operator  Result
& Logical AND
Logical OR
Logical XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
Logical unary NOT
&= AND assignment
|=  OR assignment
^=  XOR assignment
==  Equal to
!=  Not equal to
?: Ternary if-then-else

These logical operators operate on the values same way they operate on bits of integers. Below is the table showing the effect of these operations.

X Y X|Y X&Y X^Y !X
FALSE FALSE FALSE FALSE FALSE TRUE
TRUE FALSE TRUE FALSE TRUE FALSE
FALSE TRUE TRUE FALSE TRUE TRUE
TRUE TRUE TRUE TRUE FALSE FALSE

Here is the example to show the use of boolean logical operators.

class opTest {
public static void main(String args[]) {
        boolean x = true;
        boolean y = false;
        boolean a = x | y;
        boolean b = x & y;
        boolean c = x ^ y;
        boolean d = (!x | y) & (x | !y);
        boolean e = !x;
        System.out.println(" x = " + x);
        System.out.println(" y = " + y);
        System.out.println(" x|y = " + a);
        System.out.println(" x&y = " + b);
        System.out.println(" x^y = " + c);
        System.out.println("!x&y|x&!y = " + d);
        System.out.println(" !x = " + e);
        }
}

Output:

 x = true
 y = false
 x|y = true
 x&y = false
 x^y = true
!x|y&x|!y = false
 !x = false

Assignment Operator

Assignment operator is nothing but a = sign which works same way like other programming languages. One point to consider while using assignment operator is the type of value which we are assigning it to a variable must be compatible with variable type. One other interesting fact is that we can use chain of assignment operators in Java.

class opTest {
public static void main(String args[]) {
        int a,b,c;
        a=b=c=10;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        }
}

Output:

a = 10
b = 10
c = 10

Ternary Operator

Ternary operator in Java is the ? operator. It is used to replace simple if…else statement. It is written like below syntax.

expr1? expr2  : expr3

The meaning of this statement is: expr1 is evaluated and will result into some boolean value. If it is true then expr2 will be evaluated else expr3 will be evaluated. Only restriction is that both expr2 and expr3 must be of same datatype and cannot be void. Let us look at one example to understand more.

class opTest {
public static void main(String args[]) {
        int i;
        String s;
        i = 10;
        s= i < 0 ? "Negative" : "Positive"; // get absolute value of i
        System.out.println(i + " is " + s);
        }
}

Output:

10 is Positive

Operator Precedence

Below is the table showing precedence between all the Java operators.

Operator Type Precedence       Highest
Unary
() [] .  
 
++ -- ~ !
Arithmetic
* / %  
+ -    
Shift << >>> >>  
Relational
> >= < <=
== !=    
Bitwise
& &    
^ ^    
| |    
Logical
&& &&    
|| ||    
Ternary ?: ? :    
Assignment = op=     Lowest
Explore Core Java Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Ravindra Savaram
About The Author

Ravindra Savaram is a Content Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.


DMCA.com Protection Status

Close
Close