Operators in Java
Operators in Java
Operator is a special symbol that tells the compiler to perform specific mathematical or logical Operation. Java supports following lists of operators.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Ternary or Conditional Operators
Arithmetic Operators
Given table shows all the Arithmetic operator supported by Java Language. Lets suppose variable A hold 8 and B hold 3.
Java arithmetic operator takes numerical values as operands, operates on them, and returns a single numerical value. Java Arithmetic operators are used for simple mathematic operations, they are
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
Operator | Example (int A=8, B=3) | Result |
---|---|---|
+ | A+B | 11 |
- | A-B | 5 |
* | A*B | 24 |
/ | A/B | 2 |
% | A%4 | 0 |
Relational Operators
Which can be used to check the Condition, it always return true or false. Lets suppose variable A hold 8 and B hold 3.
Operators | Example (int A=8, B=3) | Result |
---|---|---|
< | A<B | False |
<= | A<=10 | True |
> | A>B | True |
>= | A<=B | False |
== | A== B | False |
!= | A!=(-4) | True |
Logical Operator
Which can be used to combine more than one Condition?. Suppose you want to combined two conditions A<B and B>C, then you need to use Logical Operator like (A<B) && (B>C). Here && is Logical Operator.
Operator | Example (int A=8, B=3, C=-10) | Result |
---|---|---|
&& | (A<B) && (B>C) | False |
|| | (B!=-C) || (A==B) | True |
! | !(B<=-A) | True |
Truth table of Logical Operator
C1 | C2 | C1 && C2 | C1 || C2 | !C1 | !C2 |
---|---|---|---|---|---|
T | T | T | T | F | F |
T | F | F | T | F | T |
F | T | F | T | T | F |
F | F | F | F | T | T |
Assignment operators
Which can be used to assign a value to a variable. Lets suppose variable A hold 8 and B hold 3.
Operator | Example (int A=8, B=3) | Result |
---|---|---|
+= | A+=B or A=A+B | 11 |
-= | A-=3 or A=A+3 | 5 |
*= | A*=7 or A=A*7 | 56 |
/= | A/=B or A=A/B | 2 |
%= | A%=5 or A=A%5 | 3 |
=a=b | Value of b will be assigned to a |
Ternary operator
If any operator is used on three operands or variable is known as ternary operator. It can be represented with " ?: "
Ternary operator Read in detailTernary Operator in C