Operator-in-Java 7
Operator-in-Java 7
Topperworld.in
Operator in Java
Arithmetic Operators :
Operator Description
+ ( Addition ) This operator is used to add the value of the
operands.
Example
Let us look at a simple code that in which all the arithmetic operators are used.
// Addition Operator
result = num1 + num2;
System.out.println("Addition: " + result);
// Subtraction Operator
result = num1 - num2;
System.out.println("Subtraction: " + result);
// Multiplication Operator
result = num1 * num2;
System.out.println("Multiplication: " + result);
// Division Operator
result = num1 / num2;
Java Programming
// Modulus Operator
result = num1 % num2;
System.out.println("Modulus: " + result);
// Increment Operator
num1++;
System.out.println("Increment: " + num1);
// Decrement Operator
num2--;
System.out.println("Decrement: " + num2);
}
}
OUTPUT
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0
Increment: 11
Decrement: 4
Assignment Operators :
Assignment operator are used to assign new value to a variable. The left
side operand of the assignment operator is called variable and the right
side operand of the assignment operator is called value.
Operator Description
This operator is used to assign the value on the
=
right to the operand on the left.
Java Programming
Example
Let us look at a simple code in which all the assignment operators are used.
OUTPUT
10
30
Java Programming
10
200
2
0
Logical Operators :
Operator Description
This operator returns True if both the operands
&& (Logical AND)
are true, otherwise, it returns False.
Example
Let us look at a simple code that in which all the logical operators are used.
OUTPUT
False
True
True
Bitwise Operators :
The bitwise operator operates on bit string, binary number, or bit array. It
is fast and simple and directly supported by the processor. The bitwise
operation is also known as bit-level programming.
Operator Description
This operator takes two numbers as operands
& (Bitwise AND)
and does AND on every bit of two numbers.
Example
Let us look at a simple code that in which all the bitwise operators are used.
// Bitwise OR Operator
result = num1 | num2;
System.out.println("Bitwise OR: " + result);
OUTPUT
Bitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise Complement of num1: -11
Left Shift of num1: 40
Right Shift of num1: 2
Unsigned Right Shift of num1: 2
Ternary Operators :
Example
OUTPUT
Relational Operators :
Relational operator compares two numbers and returns a boolean value. This
operator is used to define a relation or test between two operands.
Operator Description
This operator returns True, if the value of the
< (Less than) left operand is less than the value of the right
operand, else it returns False.
This operator returns True, if the value of the
> (Greater than) left operand is greater than the value of the right
operand, else it returns False.
This operator returns True, if the value of the
<= (Less than or equal to) left operand is less than or equal to the value of
the right operand, else it returns False.
This operator returns True, if the value of the
>= (Greater than or equal to) left operand is greater than or equal to the value
of the right operand, else it returns False.
Example
Let us look at a simple code that in which all the realational operators are used.
OUTPUT
true
false
true
false
false
true
Java Programming
>= += -= *= /= %= >>=
Assignment Right to left
<<= &= ^= |=