Operators & Assignments in Java
Increment & Decrement Operators
Increment
a) Pre-increment( e.g.: y=++x;)
b) Post-increment(e.g.: y=x++;)
Decrement
a) Pre-decrement(e.g.: y=--x;)
b) Post-decrement(e.g.: y=x--;)
Arithmetic Operators(+, -, *, /, %)
If we Apply arithmetic operator between two variable a and b the result type is always.
max(int, type of a, type of b)
Eg.:
byte+byte=int
byte+short=int
short+short=int
byte+long=long
long+double=double
float+long=float
char+char=int
Represent infinity in integral arithmetic and floating point arithmetic number
In integral arithmetic (byte,short,int,long) there is no way to represent infinity , hence if infinity is the result , then we will get Arithmetic Exception in Integral arithmetic.
Eg.: SOP(100/0);// Exception in thread "main" java.lang.ArithmeticException: / by zero
But in floating point arithmetic (float and double) there ia a way to represent infinity.
Float and Double classes contains two constant:
POSITIVE_INFINITY
NEGATIVE_INFINITY
Hence, even though result is infinity we won’t to get any Arithmetic Exception in floating point arithmetic.
Eg.:
SOP(100/0.0);// Infinity
SOP(-100/0.0);// -Infinity
NAN(not a number)
In integral arithmetic (byte,short,int,long) there is no way to represent undefine result hence,if the
result is undefined we will get runtime exception saying Arithmetic Exception.
SOP(0/0);// Exception in thread "main" java.lang.ArithmeticException: / by zero
But in floating point arithmetic (float, double) there ia a way to represent undefine results. For these Float and Double classes contains NaN constant hence, if the result is undefine we won’t get any arithmetic exception in floating point arithmetic.
SOP(0.0/0);// NaN
Relational Operators(<, <=, >, >=)
Relational operators like <, <=, > and >= are key in Java for comparing values, enabling the code to make decisions and effectively control the flow based on these comparisons. They are essential for implementing logic in conditional statements and loops.
Conditional statements in java : if else, for loop, while loop.
SOP(10 <20);//true
SOP(‘a’ < 10);//false
SOP(‘a’ < 97.6);//true
SOP(‘a’ > ‘A’);//true
SOP(true > false);//CTE
Note: We can apply relational operators for every preimitive data types except boolean type;
SOP(“Hello” > “World”);//CTE
SOP(10<20<30);//CTE
Note: Listing of Relational Operators is not allowed otherwise we will get compile time error.
Equality Operators(==, !=)
We can apply Equality operators for every primitive type including Boolean type also.
E.g.:
SOP(10 == 20);//false
SOP(‘a’ == ‘b’);//false
SOP(‘a’ != ‘c’);//true
SOP(false != false);//false
We can apply equality operators for Object type also.
E.g.:
Demo d1 = new Demo();
Demo d2 = new Demo();
SOP(d1 == d2);//false
Bitwise Operators(&, |, ^)
We can apply bitwise operators for Integeral and Boolean types.
& (AND)-> Return true iff both operands are true.
|(OR) -> Return true iff atleast one operand is true.
^ (XOR)-> Returns true iff both operands are different.
^ (XOR)-> Returns true iff both operands are different.
E.g.:
SOP(true & false);//false
SOP(true | false);//true
SOP(true ^ false);//true
SOP(4 & 5);//4
SOP(4 | 5);//5
SOP(4 ^ 5);//1
Boolean Complement Operators(!)
We can apply this operator only for Boolean but not for Integral type.
SOP(!false);//true
SOP(!4);//CTE
Short Circuit Operators(&&, ||)
(x && y) -> y will be evaluated iff x is true.
(x || y) -> y will be evaluated iff x is false.
| &,| | &&,|| |
|---|---|
| Both argument should be evaluated always. | Second argument evaluation is optional. |
| Relatively performance is low. | Relatively performance is high. |
| Applicable for both Boolean and Integral types. | Applicable only for Boolean but not for Integral types. |
E.g.:
Note: In the above example if we replace && with & then we will get RTE saying Arithmetic Exception / by Zero.