Java Operators

Like other programming languages, there are arithmetic operators, bitwise operators and logical operators in Java.

Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.

The following table lists all the arithmetic operators in Java.

ath-opertorsThe following program illustrates the increment operator.

// Demonstrate ++. 
class IncDec {
    public static void main(String args[]) {
        int a = 1;
        int b = 2;
        int c;
        int d;
        c = ++b;
        d = a++;
        c++;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println("d = " + d);
    }
}

The output of this program follows:

a = 2
b = 3 
c = 4 
d = 1

The Bitwise Operators

Java defines several bitwise operators that can be applied to the integer types, long, int, short, char, and byte. These operators act upon the individual bits of their operands.

bitwise-operator

The Bitwise Logical Operators

The bitwise logical operators are &, |, ^, and ~. The following table shows the outcome of each operation. In the discussion that follows, keep in mind that the bitwise operators are applied to each individual bit within each operand.

bitwise

The Left Shift

The left shift operator, <<, shifts all of the bits in a value to the left a specified number of times.

It has this general form:
          value << num

Here, num specifies the number of positions to left-shift the value in value. That is, the << moves all of the bits in the specified value to the left by the number of bit positions specified by num..

The following program illustrates the explanation more clearly.

// Left shifting a byte value. 
class ByteShift {
    public static void main(String args[]) {
        byte a = 64, b;
        int i;
        i = a << 2;
        b = (byte) (a << 2);
        System.out.println("Original value of a: " + a);
        System.out.println("i and b: " + i + " " + b);
    }
}

The output generated by this program is shown here:

Original value of a: 64 
i and b: 256 0

The Right Shift
The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times.

Its general form is shown here:
           value >> num
Here, num specifies the number of positions to right-shift the value in value. That is, the >> moves all of the bits in the specified value to the right the number of bit positions specified by num.

The following code fragment shifts the value 32 to the right by two positions, resulting in a being set to 8:

int a = 32;
a = a >> 2; // a now contains 8


When a value has bits that are ―shifted off, those bits are lost. For example, the next code fragment shifts the value 35 to the right two positions, which causes the two low-order bits to be lost, resulting again in a being set to 8.

int a = 35;
a = a >> 2; // a still contains 8

Looking at the same operation in binary shows more clearly how this happens:

00100011     35 >> 2
1000

Bitwise Operator Compound Assignments

a = a >> 4;
a >>= 4;

The following two statements, which result in a being assigned the bitwise expression a OR b, are equivalent:

a = a | b;
a |= b;

Relational Operators

The relational operators determine the relationship that one operand has to the other. Specifically, they determine equality and ordering.

logical-op

Boolean Logical Operators

The Boolean logical operators shown here operate only on boolean operands. All of the binary logical operators combine two boolean values to form a resultant boolean value.

logical-operationHere is a program that is almost the same as the BitLogic example shown earlier, but it operates on boolean logical values instead of binary bits:

// Demonstrate the boolean logical operators. 
class BoolLogic {
    public static void main(String args[]) {
        boolean a = true;
        boolean b = false;
        boolean c = a | b;
        boolean d = a & b;
        boolean e = a ^ b;
        boolean f = (!a & b) | (a & !b);
        boolean g = !a;
        System.out.println(" a = " + a);
        System.out.println(" b = " + b);
        System.out.println(" a|b = " + c);
        System.out.println(" a&b = " + d);
        System.out.println(" a^b = " + e);
        System.out.println("!a&b|a&!b = " + f);
        System.out.println(" !a = " + g);
    }
}

The output of the program is:

a = true 
b = false 
a|b = true 
a&b = false 
a^b = true 
a&b|a&!b = true 
!a = false

After running this program, you will see that the same logical rules apply to boolean values as they did to bits. As you can see from the following output, the string representation of a Java boolean value is one of the literal values true or false.

The ? Operator

Java includes a special ternary (three-way) operator that can replace certain types of if-then-else statements. This operator is the ? operator.
The ? has this general form:

expression1 ? expression2 : expression3

Here, expression1 can be any expression that evaluates to a boolean value. If expression1 is true, then expression2 is evaluated; otherwise, expression3 is evaluated.

Type Conversion and Casting
Selection Statement - Decision Making