JAVA
Operators in java tutorials

The variables, which are declared and defined, are the operands which are operated upon by the operators.

Operators specify what operations are to be performed on the operands.

Some operators require two operands, while few operators require only one operand.

Java includes many operators, which fall into different categories as below:

Arithmetic Operators

Unary Operators

Relational Operators

Assignment Operators

Logical Operators

Bitwise Operators
Arithmetic Operators

There are 5 arithmetic operators in Java

Following table shows all the arithmetic operators supported by Java. Assume variable A holds 20 and variable B

holds 30 than:
Operator 
Description 
Example 
+ 
Adds two operands 
A + B will give 50 
 
Subtracts second operand from the first 
A  B will give 10 
* 
Multiply both operands 
A * B will give 600 
/ 
Divide numerator by denumerator 
A / B will give 0 
% 
Modulus Operator and remainder of after an integer division 
A % B will give 20 
Unary Operators

As operator’s works on single operands, they are called unary operators.

Most common unary operator is unary minus, where a minus sign precedes a numeric variable or expression.

Unary minus operator is different from arithmetic minus (Substation). Arithmetic substation or minus operator requires two operands.
Example
200
 ( a + b )
 4 * ( 2 + 3 )

Other Unary Operator

Increment operator ++The increment operator ++adds 1 to operand.

Decrement operator   The decrement operator  subtracts 1 to operand.

Both Increment and Decrement operator can be used in two different ways.

Pre: If unary operator precedes the operand for example (++ operand or   operand), then value of operand will be increased / decreased by one, before it is utilized within program.

Post: If unary operator follows the operand for example (operand++ or operand ), then value of operand will be increased / decreased by one, after the execution of statement within program.
Relational Operators

The relational operators allow you to compare two values.

You can determine if the two values are equal or not and can check which of the two values is the larger.

All of the relational operations return a Boolean result.

In the case of the 'equal to' and 'not equal to' operations, any two compatible data types can be compared.

However, for the comparison operators that determine if one value is greater than another, the data type must support the concept of ordering.

This includes all of the basic numeric types but not Boolean values as it is not sensible to consider 'true' to be either greater than or less than 'false'.
Operator 
Meaning 
< 
Less than 
> 
Greater than 
= = 
Equal to 
< = 
Less than or Equal to 
> = 
Greater than or Equal to 
! = 
Not equal to 
Assignment Operators

Simple assignment operators: The equal (=) sign is used for assigning a value to variable. The lefthand side has to be a variable and righthand side of equal sign has to be value of variable.
Example:
a = 5; // this statement will assign 5 to variable a.
b = 2 + 10; // this statement will solve right hand side equation and assign 12 to b.
x = y = z = 20; //this example of multiple assignment, it will assign 20 to x, y and z.

Shorthand assignment operator: The plus equal (+=), minus equal (=), multiply equal (*=), divide equal (/=), modulo equal (%=), the use of shorthand assignment operator has three advantages listed below:

What appears on the lefthand side need not be repeated and therefore it becomes easier to write. The statement is more concise and easier to read.

You can also use shorthand assignment operator as follow. Assume A = 5 and B=2 then check following
Operator 
Regular Expression 
Shorthand assignment,Expression can be written as 
Result 
+ = 
A = A + B ; 
A += B; 
7 
 = 
A = A – B ; 
A  = B; 
3 
* = 
A = A * B ; 
A * = B; 
10 
/ = 
A = A / B ; 
A / = B; 
2 
% = 
A = A % B ; 
A % = B; 
1 
Logical Operators

The logical operator && logical AND,  meaning OR, ! meaning NOT.

The logical operator &&and  are used when we want to test more than one condition and makes decisions.

When result of both operands are true (T), && (AND) operators gives true result, otherwise it gives false.

When result of any operands is true (T),  (OR) operators gives true result, otherwise it gives false.

( ! ) NOT operators reverse Boolean result.

Truth table for each logical operators are below:
Result of A
Condition 
Result of BCondition 
AND conditionA && B 
OR conditionA  B 
Not Condition! A 
T 
T 
T 
T 
F 
T 
F 
F 
T 
F 
F 
T 
F 
T 
T 
F 
F 
F 
F 
T 
Bitwise Operators

Bitwise operator works on bits and performs bit by bit operation. The truth tables for &&, , and ^ are as Follows:
p 
q 
p && q 
p  q 
p ^ q 
0 
0 
0 
0 
0 
0 
1 
0 
1 
1 
1 
1 
1 
1 
0 
1 
0 
0 
1 
1 
Assume if a=50 and b=15 now in binary format they will be shown as below:
a = 110010
b = 1111
Now let apply Binary operator in a and b
110010
1111
   
a &b = 0010 which is 2
a  b = 1111 which is 63
a ^b = 1101 which us 61

The Bitwise operators supported by Java are listed in the following table. Assume variable A holds 60 and variable B holds 13 than:
Operator 
Description 
Example 
& 
Binary AND Operator copies a bit to the resultif it exists in both operands. 
(A & B) will give 12 which is 0000 1100 
 
Binary OR Operator copies a bit if it exists ineither operand. 
(A  B) will give 61 which is 0011 1101 
^ 
Binary XOR Operator copies the bit if it is set inone operand but not both. 
(A ^ B) will give 49 which is 0011 0001 
~ 
Binary Ones Complement Operator is unaryand has the effect of 'flipping' bits. 
(~A ) will give 60 which is 1100 0011 
<< 
Binary Left Shift Operator. The left operandsvalue is moved left by the number of bitsspecified by the right operand. 
A << 2 will give 240 which is 1111 0000 
>> 
Binary Right Shift Operator. The left operandsvalue is moved right by the number of bitsspecified by the right operand. 
A >> 2 will give 15 which is 0000 1111 