Basic Java Operators
These are the types of operators in java:
- Arithmetic Java operators: These are applied to perform arithmetical calculations like addition, subtraction, multiplication, division, modulus (Returns the remainder).
Mathematical expression: abc
Java expression: a*b*c
Unary increment and decrement operators:
These operators increase or decrease the value of an operand by 1, respectively.
Prefix: When the increment or decrement operator is applied before the operand we call it prefix operator. In this case, we change the value of the variable before the operation.
Example:
- int x=5;
x=++x;
So,the final value of x is 6.
- int x=5;
x=–x;
In this case the value of x becomes 4.
Postfix: When the increment or decrement operator is applied after the operand we call it prefix operator. In this case, we change the value of the variable after the operation.
Example:
- int x=5;
x= x++ *4;
In this case x remains 5 before multiplication.So, we get 20 stored in x.
2.int x=5;
x=x– *5;
In this case, x remains five(5) before multiplication. So, the final value of x is 25.
- Relational Java operators: As the name suggests, these operators show a relationship among the operands. They result in either True or False.
1.Less than (<) : Example: 3<7 Results in True
2.Greater than(>):Example: 3>7 Results in False
3.Greater than or equal to(>=):Example:6>=6 Results in True
4.Less than or equal to(<=): Example: 7<=12 Results in True
5.Equal to(==):Example: 7==7 Results in True
6.Not equal to(!=):Example: 7!=7 Results in False
3.Logical Operators: Java uses AND(&&),OR(||) or NOT(!).
Logical AND: It results in true if both expressions are valid else false.
Example:
6>8 && 4!=5
This will result in false because the first condition is false.
Logical OR: It results in true if either condition is true.
Example:
6>8 || 4!=5
This will result in true because the first condition is true.
Logical NOT: It will simply revert the outcome of the expression.
Example:
!(10==11)
This will result in true.
4.Bitwise operators: These perform operations on bit level of operands. These can be applied to byte, short, int, long and char.
Bitwise AND(&): It will give high(1)if both operands are high else low(0).
Example of Java Operators using Bitwise Operators:
1&0 results in 0.
Bitwise OR(|): It will give high if either operand is high.
Eg.
1|0 results in 1.
Bitwise complement(~): It will give the balance.
Bitwise XOR(^): It results in high if we have a different value of operands and lows if we have the same value of operands.
Eg.
1^0 results in 1.
0^0 results in 0.
Left shift(<<):
Example:int x=3;
x<<2;
Now the binary of 3 is 11.
We need to shift it left by 2 bits.
So, the final value becomes 1100, whose decimal equivalent is 12.
Right shift:
Example:
int x=12;
x>>2;
Now the binary of 12 is 1100.
We need to shift it right by 2 bits.
So, the final value becomes 11, whose decimal equivalent is 3.
Zero fill right shift (<<<):
Example:
int x=14;
x<<<2;
Now the binary of 14 is 1110
Shift the value right by two bits and fill the shifted values with 0. So, the value comes out to
be 3.
5.Assignment operators:
= It assigns the value like a=10;
+= Example: int a=1;a+=4 this results in change in value of a to 5
-= Example: int a=4;a-=1;this results in change in value of a to 3.
*= Example:int a=2;a*=3;this results in change in value of a to 6.
/= Example: int a=4;a/=1;this results in change in value of a to 4.
%= Example:int a=8;a%=3;this results in change in value of a to 2.
<<= a<<=2 is same as a=a<<2
>>= a>>=2 is same as a=a>>2
&= a&=3is same as a=a&2
6.Miscellaneous
Ternary operator(Conditional Assignment)
They deal with three operands.
Syntax:
Variable=(test expression)?value if true:value if false.
Eg.
int a=12;b=13;
int c=(a==b)?12:13;
Since the expression is false so c will store value 13.
If you have any questions or suggestion about the topic Java Operators, please feel to contact me through our contact page.
Next Topic: Java Modifiers
About The Basic Operators In Java
Project Name: | Basic Operators |
Language/s Used: | JAVA |
Database: | None |
Type: | Desktop Application |
Developer: | IT SOURCECODE |
Updates: | 0 |