Operators & Assignments
____________________________________________
____________________________________________
Operator - An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
There are two types of increment operators
int x = ++y; // value is incremented first and then result is computed.
2. Post Increment - value is first used for computing the result and then incremented.
NaN (Not a Number) - In integral arithmetic, there is no way to represent unrefined result, hence if the result is undefined we will get Arithmetic Exception
System.out.print(0/0) // Arithmetic Exception : 1 by 0
but in case of floating point arithmetic, there is way to represent undefined result, for this float & double classes contains NaN Constance, hence even though the result is undefined we won't get any run time exception.
System.out.print(0/0.0);
The only overloaded operator in Java is + operator, sometimes it acts as arithmetic addition operator and sometimes act String arithmetic operator or string concatenation operator.
int a = 10, b = 20, c = 30;
String s = "arjava";
System.out.print(a+b+c+d); // Output - 60arjava
System.out.print(a+b+d+c); // Output - 30arjava30
System.out.print(d+a+b+c); // Output - arjava102030
System.out.print(a+d+b+c); // Output - 10arjava2030
Note: If first value is integer then it will act as addition otherwise if first value is String it will act as concatenation.
we can apply this operator to all primitive data types except boolean
we can apply this operator to all primitive data types including boolean. we can also use this operator for object reference, it return true if both r1 == r2 are pointing same object, here equality operator is always meant for reference or address comparison.
To apply equality between the object reference, there must be some relationship between arguments types either parent to child or child to parent otherwise we will
get compile time error : Incompatable type
using this operator we can check, whether the given object is of particular type or not, to use this, there must be some relationship between arguments types either parent to child or child to parent otherwise we will get compile time error : Incompatable type
x instanceOf P // x = reference type, P = class or interface
& -> AND if both operands are true then the result is true
| -> OR if at least one operand is true then the result is true
^ -> if both operands are different then the result is true
& |
&& ||
There are two types of primitive type casting
1. Implicit Type Casting - also known as Widening or Up casting
2. Explicit Type Casting - also known as Narrowing or Down Casting
There are three types of assignment operator
1. Simple assignment operator - int x = 11;
2. Chained assignment operator -
int a, b, c, d;
a = b = c = d = 20;
we can't perform chained assignment at the time of declaration
3. Compound assignment operator -
int a = 10;
a += 30
System.out.print(a) // Output - 40
These all are compound assignment operator += -= %= *= /= &= |= ^= >>= >>>= <<=
also known as Ternary Operator
int a = 10, b = 20;
int x = (a>b) ? 40:50;
System.out.print(x) // Output - 50
we can use this operator to create the object, there is no delete operator in java because destroy of useless object in java is responsibility of Garbage Collection