The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. ― Donald E. Knuth

# Java Tutorial

## Java Operators

 Module Index ► Arithmetic Operators ► Logical Operators ► Relational Operators ► Bitwise Operators ► Special Operators ► Operator Precedence and Associativity ► Postfix and Prefix Operators ► Typecasting

Operators are used to perform mathematical & logical operations. Operators supported by Java can be categorized into arithmetic, logical, relational, bitwise (used for bit manipulation) and some special operators. Let's see how these operators are used in programming.

Arithmetic Operators
Arithmetic Operators supported by Java are ADD ( + ), SUBTRACT ( - ), MULTIPLY ( * ), DIVIDE ( / ),
MODULO ( % ), INCREMENT ( ++ ) and DECREMENT ( -- )
.
' / ' operator prints the quotient of the division operation and ' % ' operator prints the remainder of the division operation. For e.g, 5 / 2 = 2 and 5 % 2 = 1.
' ++ ' operator increments a value by 1 and ' -- ' operator decrements a value by 1. For e.g 5++ = 6 and 5-- = 4.
The behaviour of ' + ', ' - ' and ' * ' are trivial.
Let's look at the following program to see arithmetic operators in action :

Please note that some of the expressions in which a variable is present in both sides can be written as :

 Expression is Equivalent to X = X + Y X += Y X = X - Y X -= Y X = X * Y X *= Y X = X / Y X /= Y X = X % Y X %= Y

Logical Operators
Logical Operators supported by Java are AND ( && ), OR ( || ) and NOT ( ! ).
Consider two operands A and B.
A && B evaluates to True only if both A and B are True.
A || B evaluates to True if any of the two operands is True.
' ! ' operator negates the state of an operand. For e.g, if A = True, then !A = False.
Note that any non-zero value is considered True and zero is considered False.
Following program shows logical operators in action :

Relational Operators
Relational operators ( used for comparison operations ) supported by Java are EQUAL ( == ),
NOT EQUAL ( != ), GREATER THAN ( > ), LESS THAN ( < ), GREATER THAN OR EQUAL TO ( >= ) and LESS THAN OR EQUAL TO ( <= )
.
Following program illustrates the behaviour of relational operators :

Bitwise Operators
Bitwise Operators ( used for bit manipulation ) supported by Java are BITWISE AND ( & ), BITWISE OR ( | ), BITWISE XOR ( ^ ), BITWISE NOT ( ~ ), LEFT SHIFT ( << ), RIGHT SHIFT ( >> ) and RIGHT SHIFT WITH ZERO FILL ( >>> ). These operators operate at bit level.
Consider two bits bit1 and bit2.
bit1 & bit2 evaluates to True if and only if both bit1 and bit2 are True.
bit1 | bit2 evaluates to True if any of the two bits is True.
bit1 ^ bit2 evaluates to True if and only if one of the bits is True.
~ operator toggles a bit. If bit1 = True, the ~bit1 = False.
' A << 2 ' left shifts the bits of a number (A) by 2 positions.
' A >> 1 ' right shifts the bits of a number (A) by 1 position.
' A >>> 1 ' right shifts the bits of a number (A) by 1 position and shifted values are filled up with zeroes.

Difference between >> and >>> operators
There is no difference between the two operators when we are dealing with positive integers. The difference is observed in case of negative integers. The Most Significant Bit ( MSB ) or the leftmost bit is 1 in case of negative integers ( i.e MSB is the sign bit ). Now, when we use >> on a negative integer, the sign bit is not shifted but if we use >>> operator, the sign bit is also shifted along with other bits and the position of sign bit is filled with 0 along with other shifted values.

Suppose A = 7 and B = 19. In binary format : A = 00000111 and B = 00010011
Following table shows how bitwise operators do bit manipulation :

 Binary A 0 0 0 0 0 1 1 1 B 0 0 0 1 0 0 1 1 A & B 0 0 0 0 0 0 1 1 A | B 0 0 0 1 0 1 1 1 A ^ B 0 0 0 1 0 1 0 0 ~A 1 1 1 1 1 0 0 0 A << 2 0 0 0 1 1 1 0 0 A >> 1 0 0 0 0 0 0 1 1

Following program demonstrate the behaviour of bitwise operators :

Note that in the above program, there is no demonstration of NOT ( ~ ) operator since the result would be unexpected and will confuse you. You might have noticed that all decimal numbers are represented using 8 bits
( 1 Octet ) but in the system memory it is stored as a sequence of multiple octets (4 in case of 32 bit systems). In our case, other 3 octets would be all 0s. But NOT operation would toggle all those 0s to 1s which would result into a very high value ( outside the range of int ). As a result, some unexpected value would be displayed.

Special Operators

 instanceof This operator is applicable to objects only. It checks whether an object is of particular class type. Condition Conditional Operator returns a value depending on a condition. Syntax : ( Condition ? a : b ). If condition is true, a is returned else b is returned. Assignment It is used to assign one value to other in an expression. In the expression c = a + b, value of ( a + b ) is assigned to c.

Following program illustrates the use of conditional operator :

Following program illustrates the use instanceof operator :

The instanceof operator is not used much and the above program doesn't reflect a practical use case. It can be useful in knowing the actual class of an object when we are using inheritance, a feature which we will discuss later in the tutorial.

Operator Precedence & Associativity
Precedence of operators determines the order of evaluation of expression consisting of multiple operators.
Higher precedence operators will be evaluated first. Associativity determines whether an expression is evaluated left to right or right to left. Consider the expressions below :

 x = 2 + 3 * 2 => x = 2 + 6 => x = 8 ( '*' has higher precedence than '+' ) y = 2 * 3 / 2 => y = 6 / 2 => y = 3 ( '*' and '/' has same precedence but associativity is left to right ) z = 3 / 2 * 2 => z = 1 * 2 => z = 2 ( '*' and '/' has same precedence but associativity is left to right )

Following table shows the precedence and associativity of the C++ operators ( Operator precedence decreases as we move downwards ) :

 Operators Precedence Postfix ( ++, -- ) Left to Right Unary ( +, -, !, ~), Prefix ( ++, -- ) Right to Left *, /, % Left to Right Additive ( +, - ) Left to Right Left and Right Shift ( <<, >>, >>> ) Left to Right Relational ( <, <=, >, >= ) Left to Right Equality ( ==, != ) Left to Right Bitwise AND ( & ) Left to Right Bitwise XOR ( ^ ) Left to Right Bitwise OR ( | ) Left to Right Logical AND ( && ) Left to Right Logical OR ( || ) Left to Right Conditional ( Condition ? x : y ) Right to Left Assignment ( = ) Right to Left

*Note : Unary operators have single operand

Postfix and Prefix Operators
The increment (++) and decrement (--) operators can appear either on left side (prefix) or right side (postfix) of the operand. Following program demonstrates the behaviour of postfix and prefix operators :

Typecasting
Sometimes it is required to explicitly convert a variable from one data type to another. This concept is known as typecasting. Following program will make it clear.

In the above program, both a and b are integers. Now, ( a / b ) = ( 7 / 3 )= 2 . Now, since x is a float type variable, so x stores 2.000000 . In case of y , variable a is converted to float and thus the expression is evaluated as
y = (float)a / b = ( 7.000000 / 3 ) = 2.333333. In case of z, the value of ( a / b ) is converted to float and the expression is evaluated as y = (float) ( a / b ) = float ( 7 / 3 ) = float ( 2 ) = 2.000000.

Automatic Type Conversion
In Java, we can mix variables and constants of different data types in an expression. During evaluation, operands of lower type is automatically converted to higher type. For e.g, If an expression consists of byte, int and float operands, the result is always promoted to float.