Java operators are symbols that is used to perform mathematical or logical manipulations. Java is rich with built-in operators.
There are many types of operators available in Java such as:
Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment Operators and Misc Operators.
Arithmetic Operators
Operator | Description |
---|---|
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
++ | Increment |
−− | Decrement |
Relational Operators
Operator | Description |
---|---|
== | Is equal to |
!= | Is not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Logical Operators
Operator | Description |
---|---|
&& | And operator. Performs a logical conjunction on two expressions. (if both expressions evaluate to True, result is True. If either expression evaluates to False, result is False) |
|| | Or operator. Performs a logical disjunction on two expressions. (if either or both expressions evaluate to True, result is True) |
! | Not operator. Performs logical negation on an expression. |
Bitwise Operators
Operator | Description |
---|---|
<< | Binary Left Shift Operator |
>> | Binary Right Shift Operator |
>>> | Shift right zero fill operator |
~ | Binary Ones Complement Operator |
& | Binary AND Operator |
^ | Binary XOR Operator |
| | Binary OR Operator |
Assignment Operators
Operator | Description |
---|---|
= | Assign |
+= | Increments, then assigns |
-= | Decrements, then assigns |
*= | Multiplies, then assigns |
/= | Divides, then assigns |
%= | Modulus, then assigns |
<<= | Left shift and assigns |
>>= | Right shift and assigns |
&= | Bitwise AND assigns |
^= | Bitwise exclusive OR and assigns |
|= | Bitwise inclusive OR and assigns |
Programs to Show How Assignment Operator Works
Example:
import java.util.*;
import java.lang.*;
import java.io.*;
public class arithop {
public static void main(String[] args) {
int number1 = 6;
int number2 = 8;
int sum = number1 + number2;
int dif = number1 - number2;
int mul = number1 * number2;
int quot = number1 / number2;
int rem = number1 % number2;
System.out.println("number1 is : " + number1);
System.out.println("number2 is: " + number2);
System.out.println("Sum is: " + sum);
System.out.println("Difference is : " + dif);
System.out.println("Multiplied value is : " + mul);
System.out.println("Quotient is : " + quot);
System.out.println("Remainder is : " + rem);
}
}
Misc Operators
Java supports some special operators such as:
Operator | Description |
---|---|
Conditional(Ternary) Operator ( ? : ) | Operator is used to decide which value should be assigned to the variable. |
instanceOf Operator | Object reference variables |
Programs to Show How Conditional Operator Works
Example:
import java.util.*;
import java.lang.*;
import java.io.*;
public class optexp {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 40 : 60;
System.out.println("Value of b is : " + b);
b = (a == 10) ? 20 : 40;
System.out.println("Value of b is : " + b);
}
}
0 comments:
Post a Comment