Java provides a rich operator environment to write the program.
Java also defines some additional operators for certain special cases.
 Arithmetic Operators: are used in mathematical expressions in the java code.
What are the arithmetic operators in java?
Operator
 Description

+ 
Addition 
 
Subtraction or unary minus 
* 
Multiplication 
/ 
Division 
% 
Modulus 
++ 
Increment 
 
Decrement 
+= 
Addition assignment 
= 
Subtraction assignment 
*= 
Multiplication assignment 
/= 
Divison assignment 
%= 
Modulus assignment 
Example Program for arithmetic operators in java:
// Arithmetic operations in java
class ArithmeticTest {
public static void main(String args[]) {
int a = 5, b = 4, c = 2;
System.out.println("Addition of " + a + " and " + b + ": " + (a + b));
System.out.println("Subtraction of " + a + " and " + b + ": " + (a  b));
System.out.println("Multiplication of " + a + " and " + b + ": " + (a * b));
System.out.println("Division of " + a + " and " + b + ": " + ((double)a / (double)b));
System.out.println("Modulus of " + a + " and " + b + ": " + (a % b));
//Increment by 1
int d = ++a;
//Decrement by 1
int e = b;
//Addition assignment
a += 1;
// Subtraction assignment
b = 1;
System.out.println("Increment of a:" + d);
System.out.println("Decrement of b:" + e);
System.out.println("Addition assignment of a:" + a);
System.out.println("Subtraction assignment of b:" + b);
}
}
Output:
Addition of 5 and 4: 9
Subtraction of 5 and 4: 1
Multiplication of 5 and 4: 20
Division of 5 and 4: 1.25
Modulus of 5 and 4: 1
Increment of a:6
Decrement of b:3
Addition assignment of a:7
Subtraction assignment of b:2
 Bitwise Operators: can be applied to the integer types: long, int, short and byte.
These operators work upon the individual bits of their operands.
What are the bitwise operators in java?
Operator
 Description (Bitwise)

~ 
Unary NOT 
& 
Unary AND 
 
Unary OR 
^ 
Exclusive OR (XOR) 
>> 
Shift right 
>>> 
Shift right fill 
<< 
Shift left 
&= 
AND assignment 
= 
OR assignment 
^= 
exclusive OR assignment 
>>= 
Shift right assignment 
>>>= 
Shift right zero fill assignment 
<<= 
Shift left assignment 
Example program for bitwise operators in java:
//Bitwise operations in java
class BitwiseOperatorsTest {
public static void main(String args[]) {
int a = 5, b = 4, c = 2;
System.out.println("Bitwise " + a + " AND " + b + ": " + (a & b));
System.out.println("Bitwsie of " + a + " OR " + b + ": " + (a  b));
System.out.println("Bitwise of " + a + " XOR " + b + ": " + (a ^ b));
System.out.println("Bitwise Unary NOT of" + a +": " + ~a);
System.out.println("Shift right by 2 of " + a + " : " + (a >> 2));
//Shift right zero fill
int d = a >>> 2;
//Shift left by 3
int e = b << 3;
//Bitwise AND assignment
a &= 4;
// Shift right assignment
b >>= 3;
System.out.println("Shift right zero fill of " + a + " by 2:" + d);
System.out.println("Shift left by 3 of "+ b +": " + e);
System.out.println("Bitwise AND assignment of a and 4:" + a);
System.out.println("Shift right assignment of b by 3:" + b);
}
}
Output:
Bitwise 5 AND 4: 4
Bitwsie of 5 OR 4: 5
Bitwise of 5 XOR 4: 1
Bitwise Unary NOT of5: 6
Shift right by 2 of 5 : 1
Shift right zero fill of 4 by 2:1
Shift left by 3 of 0: 32
Bitwise AND assignment of a and 4:4
Shift right assignment of b by 3:0
 Relational Operators: Outcome of these operators is a boolean value. Relational operations are mostly used in expressions that control the control statements and various looping statements. Mostly used for comparison.
What are the relational operators in java?
Operator
 Description

== 
Equal to 
!= 
Not equal to 
> 
Greater than 
< 
Less than 
>= 
Greater than or equal to 
<= 
Less than or equal to 
Example program for relational operators in java:
//Relational operators in java
class RelationalOperatorsTest {
public static void main(String args[]) {
int a = 5, b = 4;
boolean c = a > b;
System.out.println(a + " is greater than " + b +": " + c);
if (a == 5) {
System.out.println("a value is 5");
}
if (b != 2) {
System.out.println("b values is not 2");
}
}
}
Output:
5 is greater than 4: true
a value is 5
b values is not 2
 Boolean Logical Operators: These operators are only for boolean operands.
What are the logical operators in java?
Operator
 Description

& 
Logical AND 
 
Logical OR 
^ 
Logical XOR(exclusive OR) 
 
Shortcircuit OR 
&& 
Shortcircuit AND 
! 
Logical Unary NOT 
&= 
AND assignment 
= 
OR assignment 
^= 
XOR assignment 
== 
Equal to 
!= 
Not equal to 
?: 
Ternary ifelse 
Example program for logical operators in java:
//Logical operators in java
class LogicalOperatorsTest {
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;
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: " + f);
// Short circuit AND
if (a && b) {
System.out.println("Both a and b are not true");
}
//Short circuit OR
if (a  b) {
System.out.println("Either a or b is true");
}
}
}
Output:
a: true
b: false
a & b: false
a  b: true
a ^ b: true
!a: false
Either a or b is true
 Assignment Operators: assignment operator in java is single equal sign (=).
// here var_name must be compatible with the type of expression.
var_name = expression;
Example:
int a, b, c;
// Assigning 205 value for variable a, b and c.
a = b = c = 205;

What is ternary operator?
Ternary Operator ( ? operator): Java has a special ternary(three way) operator that can replace ifelse statements for certain types.
expression1?:expression2:expression3
Example:
int a, b, c;
a = 5;
b = 2;
//Assigns 10 to variable c if a is greathan b, otherwise assigns 20 to variable c.
c = (a > b)? 10 : 20;