Learn Java Operators

Category: Java   Tags: Java, Java Basic

Java support rich set of operators. It can be grouped as:

  • Arithmetic Operators
  • Bitwise Operators
  • Relational Operators
  • Logical Operators

Arithmetic Operators

Arithmetic operators are used in mathematical expressions. The following table lists the arithmetic operators:

Operator Description
+ Addition
- Substraction
* Multiplication
% Modulus
/ Division
++ Increment
-- Decrement
+= Addition Assignment
-= Substraction Assignment
*= Multiplication Assignment
/= Division Assignment
%= Modulus Assignment

Increment and Decrement Operator

The increment operator increases its operand by one. The decrement operator decreases its operand by one. For example, this statement:

x = x + 1;

can be rewritten like this by use of the increment operator:

x++;

Similarly, this statement:

x = x - 1;

is equivalent to

x--;

Arithmetic Compound Assignment Operators

In Java a compound assignment operator can be written as:

<variable-name> <operator>= <expression>

For example += -= %= *= are compound assignment operator. Here is how it works:

x = x + 10 is equivalent of x += 10

x = x - 10 is equivalent of x -= 10

x = x % 10 is equivalent of x %= 10

x = x * 10 is equivalent of x *= 10

Here is one Java program

JavaOperatorExample.java

                          public class JavaOperatorExample {
                              public static void main(String s[]) {
                                int a = 10 + 10;
                                int b = a / 2;
                                int c = b - 2;
                                int d = c * 2;
                                System.out.println("a = " + a);
                                System.out.println("b = " + b);
                                System.out.println("c = " + c);
                                System.out.println("d = " + d);
                                System.out.println("25 mod 10 = " + 25 % 10);
                                System.out.println("-----------------------------------");

                                a = 1;
                                b = 2;
                                c = 3;
                                a += 5;
                                b *= 4;
                                c += a * b;
                                c %= 6;
                                System.out.println("a = " + a);
                                System.out.println("b = " + b);
                                System.out.println("c = " + c);
                                System.out.println("-----------------------------------");

                                a = 1;
                                b = 2;
                                c = ++b;
                                d = a++;
                                System.out.println("a = " + a);
                                System.out.println("b = " + b);
                                System.out.println("c = " + c);
                                System.out.println("d = " + d);
                                System.out.println("-----------------------------------");
                              }
                          }
                        

Output:

a = 20
b = 10
c = 8
d = 16
25 mod 10 = 5
-----------------------------------
a = 6
b = 8
c = 3
-----------------------------------
a = 2
b = 3
c = 3
d = 1
-----------------------------------
                        

The Bitwise Operators

Bitwise operators act upon the individual bits of their operands. Following table summarize bitwise operators supported by Java:

Operator Description
~ 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 Logical Operators

The bitwise logical operators are |, ^, ~ and &. Following table summarize these operation:

X Y X | Y X ^ Y X & Y ~X
0 0 0 0 0 1
1 1 1 0 1 0
0 1 1 1 0 1
1 0 1 1 0 0

Following program is a simple example of Java bitwise operators:

BitwiseOperatorDemo.java

                          public class BitwiseOperatorDemo {
                              public static void main(String s[]) {
                                  String binaryValues[] = {
                                      "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
                                      "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
                                  };
                                  int a = 1;
                                  int b = 5;
                                  int c = a & b;
                                  int d = a ^ b;
                                  int e = a | b;
                                  int f = ~a & 0x0f;
                                  System.out.println("a & b = " + binaryValues[c]);
                                  System.out.println("a ^ b = " + binaryValues[d]);
                                  System.out.println("a | b = " + binaryValues[e]);
                                  System.out.println("~a = " + binaryValues[f]);
                              }
                          }
                        

The value of ~a is ANDed with 0x0f in order to reduce its value to less than 16, so it can be printed by use of the binaryValues array.

Here is the ouput of this Java program:

                          a & b = 0001
                          a ^ b = 0100
                          a | b = 0101
                          ~a = 1110