Types of Operators
1. Arithmetic Operators (+, -, *, /, %).2.Relational Operators (<, >, <=, >=).
3. Equality Operators (==, !=).
4. Bit-wise Operators (&, |, ^).
5. Short Circuit Operators (&&, ||).
6. Assignment Operators.
6.1 Simple Assignment Operator (=).
6.2 Compound Assignment Operator (+=, -+, *=, /=, %=).
6.3 Chained Assignment Operator (a=b=c=d).
7. Increment / Decrement Operator (++, --).
8. Conditional Operator OR Turnary Operator (~).
9. Complement Operator.
10. Shift Operator.
10.1 Left Shift(<<). 10.2 Right Shift. 10.2.1 Signed (>>).
10.2.2 Unsigned (>>>).
11. String Concatenation Operator(+).
12. Instance Of Operator.
13. New Operator.
Arithmetic Operators
class Div
{
public static void main(String[] args)
{
byte a = 0;
byte b = 10;
byte c =(byte)(b/a);
System.out.println(c);
}
}
/* RUNTIME EXCEPTION:-->>
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Div.main(Div.java:8)
REMEMBER:-->> We are getting the exception always at runtime we will never get any exception at compile time.
Why we are getting this exception?
Ans. Here we are dividing 10 by 0 so we are getting the above exception.
In mathmatics if we are dividing something by zero then the result will come as Infinity
But in java for byte, short, int, long there is no concept like infinity so the JVM is rising above exception.
*/
class Div1
{
public static void main(String[] args)
{
float a = 0.0f;
float b = 10.0f;
float c = -10.0f;
float d = 0.0f;
System.out.println(b/a);
System.out.println(c/a);
System.out.println(d/a);
}
}
/* OUTPUT:-->>
Infinity
-Infinity
NaN
Why we are getting this output?
Ans. Here we dividing the 10.0 by 0 so we are getting output as "Infinity"
Dividing -10.0 by 0 so we are getting output as "-Infinity"
We cannot divide 0 by 0 si we are gette\ing output as "NaN"
"NaN stands for Not a Number"
We are getting above output because in java for float and double we have the concept like
POSITIVE_INFINITY
NEGATIVE_INFINITY
NaN
Which is already defined in the class file.
To see the class file use following command
javap java.lang.Float
javap java.lang.Double
"javap stands for java profiler" Which is use to see the content of any class file.
If we will see the class file then they have already define in the first three line from top as follow.
public static final float POSITIVE_INFINITY;
public static final float NEGATIVE_INFINITY;
public static final float NaN;
*/
Relational Operators
class RelationalOp
{
public static void main(String[] args)
{
System.out.println(10.0<10);
System.out.println('a'==97);
System.out.println('A'==68);
// To convert Integer value into ASCII value.
char i = 65;
System.out.println(i);
}
}
/* OUTPUT:-->>
false
true
false
Here we can easily say that 10.0 is not less than 10 so we are getting the output as false.
The ASCII value of the a is 97 so we are getting output as false.
A==68 we are getting output as false because the ASCII value of
A is 65 we can see the ASCII of A by using following code
char i = 65;
System.out.println(i);
The nesting of the relational operator is not possible in java
like (10.0<09<08);
We can't apply the relational operator between the object references
otherwise it will gives a compile time error.
*/
Equality Operators
class EqualityOp
{
}
class S extends EqualityOp
{
public static void main(String[] args)
{
// Creation of object
EqualityOp e1 = new EqualityOp();
EqualityOp e2 = e1;
EqualityOp e3 = new EqualityOp();
S s = new S();
// Comparison of object we will get output as true or false.
System.out.println(e1==e2);
System.out.println(e1==s);
System.out.println(e1==e3);
//Reference values of object in hexadecimal
System.out.println(e1);
System.out.println(e2);
System.out.println(e3);
System.out.println(s);
// Printing the hash code of the objects in integer
System.out.println(e1.hashCode());
System.out.println(e2.hashCode());
System.out.println(e3.hashCode());
System.out.println(s.hashCode());
//Converting integer value into hexa decimal
String b = Integer.toHexString(e1.hashCode());
System.out.println(b);
String c = Integer.toHexString(e2.hashCode());
System.out.println(c);
String d = Integer.toHexString(e3.hashCode());
System.out.println(d);
String a = Integer.toHexString(s.hashCode());
System.out.println(a);
}
}
/* OUTPUT:-->>
true
false
false
EqualityOp@1172e08
EqualityOp@1172e08
EqualityOp@cf2c80
S@1729854
18296328
18296328
13577344
24287316
1172e08
1172e08
cf2c80
1729854
boolean value
true
false
false
Actual Reference value of the object with the class name
EqualityOp@1172e08
EqualityOp@1172e08
EqualityOp@cf2c80
S@1729854
Hash Code of the object in integer
18296328
18296328
13577344
24287316
Hexadecimal Value
1172e08
1172e08
cf2c80
1729854
Equality operator can be applied between the primptive data type
and object refrences it will always return boolean value only.
We cannot use the nesting of equality operator like (e1==e2==e3)
The compiler will gives an error like.
S.java:14: incomparable types: boolean and EqualityOp
System.out.println(e1==e2==e3);
^
In above program there three ways to see the memory location of the object.
The JVM gives the random unique memory location to each objects.
We can't say at which memory location the object will be created.
The JVM has virtual memory and it is maintained by Heap Manager.
So whenever JVM will encounters a "new" keyword it will creat the object
with the reference of the class name like
EqualityOp@cf2c80
S@1729854
and gives the random unique number.
*/
Bit-wise Operators
class BitOp
{
public static void main(String[] args)
{
int a=4;
int b=5;
System.out.println(a & b);
System.out.println(5&6);
System.out.println(7&8);
System.out.println(8&9);
}
}
/* OUTPUT:-->>
4
4
0
8
Why we are getting this output?
Ans. Here we are performing the and operation between two operands
In an and operation we will get the output as a true if both the conditions are true.
First the JVM will convert integer into binary then it will perform the and operation as follow
LOGIC:-->>
a=4= 0 1 0 0
b=5= 0 1 0 1
--------
output: 0 1 0 0 = 4
5= 0 1 0 1
6= 0 1 1 0
-------
output:-0 1 0 1 = 4
7= 0 1 1 1
8= 1 0 0 0
-------
output:-0 0 0 0 = 0
8= 1 0 0 0
9= 1 0 0 1
-------
output:-1 0 0 0 = 8
*/
Increment/Decrement Operators
class Inc
{
public static void main(String[] args)
{
byte b=10;
b=b+1;
System.out.println(b);
}
}
/* COMPILE TIME ERROR
Inc.java:7: possible loss of precision
found : int
required: byte
b=b+1;
^
1 error
Why we are getting this error?
Ans.Here we are performing the addition between byte.
We will get the output in integer because the resultant is always max so we are storing the result in "b"
size of "b" is 2 byte because we have defined it as byte and we are trying to store the result
which is of 4 byte. Thats why we are getting the above compile time error.
If we are applying arithmatic operators between two operands the resultant is always max.
Here the programmer is responsiple to perform the type testing or casting.
*/
class Inc1
{
public static void main(String[] args)
{
int b=10;
byte c;
c=(byte)(b+1); // Explicit Type casting.
System.out.println(b+1);
}
}
/* OUTPUT:-->>
11
Why we are getting this output?
Ans.Here we have perform explicit type casting as c=(byte)(b+1);
We have defined variable "b" as an integer which is of 4 byte and trying to sotre in variable "c"
Which is defined as byte and size of "c" is 2 byte so here we have perform the type casting as c=(byte)(b+1);
*/
class Inc2
{
public static void main(String[] args)
{
byte b =10;
System.out.println(++b);
}
}
/* OUTPUT:-->>
11
Why we are getting this output?
Ans Here we have defined a variable "b" as byte and incrementing the variable.
The output which we wre getting is in integer but no need to perform type casting
because here the compiler is performing the type casting implicitly means automatically
because of this statement System.out.println(++b);
Here It is the responsibility of compiler to perform type casting.
It will automatically perform following operation
b=b+1;
b=(byte)(b+1);
NOTE:-->>
Need to remember the resultant of the following datatype, Here we should do the explicit type casting.
byte + byte =int;
byte + short = int;
short + short = int;
int + byte = int;
int + short = int
int + int = int
long + int = long;
int + float = float;
int + double = double;
float + float = float;
float + double = double;
double + double = double;
*/
class IncDecOp
{
public static void main(String[] args)
{
int x=4;
int y=0;
int r=5;
int q=0;
y=++x;
q=--r;
System.out.println(y);
System.out.println(q);
}
}
/*OUTPUT:-->>
5
4
y=++x; q=--r; this method is called as pre increment/decrement.
Here the value of y is incremented at the time of initialization.
So we are getting the output as 5.
In the same way the value of q initialized.
So we are getting the output as 4.
*/
class IncDecOp1
{
public static void main(String[] args)
{
int x=4;
int q=0;
int r=5;
int y=x++;
q=r--;
System.out.println(y);
System.out.println(q);
}
}
/*OUTPUT:-->>
4
5
y=x++ or q=r--; this method is called as post increment/decrement.
Here the value of y will be initialized without increment.
So we are getting the output as 4.
In the same way the value of q will be initialized without decrement.
So we are getting the output as 5.
*/
Assignment Operators
class AssignmentOp
{
public static void main(String[] args)
{
int x = 10; // Simple Assignment Operator.
byte a = 50;
a+=10; // Compound Assigment Operator.
int b,c,d,e;
b=c=d=e=40;
System.out.println(a);
b+=c-=d*=e/=2;
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}
}
/* OUTPUT:-->
60
-720
-760
800
20
Why we are getting this output?
Ans. The initial value of a is 50 and we are incrementing it by 10 so we are
getting first output as 60.
Logic behind the output of this b+=c-=d*=e/=2;
The initial value of b,c,d,e = 40
e/2 = 40/2 = 20
d*= d*e = 40*20 = 800
c-= c-d = 40-800 = -760
b+= b+c = 40+(-760) = -720
*/
class Assign
{
public static void main(String[] args)
{
float b,c,d,e;
b=c=d=e=40;
b+=c-=d/=e*=2;
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}
}
/* OUTPUT:-->
79.5
39.5
0.5
80.0
Logic:-->
e*= e*2 = 40*2 = 80
d/ = d/e = 40/80 = 0.5
c- = c-e = 40-0.5 = 39.5
b+ = b+c = 40+39.5 = 79.5
It means whenever we are performing arithmetic operation, the JVM always start
the operation from the right side.
*/
String Concatenation
class StringConcatenation
{
public static void main(String[] args)
{
String s = "Yogi";
int a = 10;
int b = 20;
int c = 30;
System.out.println(s+a+b+c);
System.out.println(a+s+b+c);
System.out.println(a+b+s+c);
System.out.println(a+b+c+s);
}
}
/* OUTPUT:->>
Yogi102030
10Yogi2030
30Yogi30
60Yogi
The only one operator can be overloaded as part of the java is + operator.
The + operator will be access as a string concatenation operation
if we apply the + operator between the two operands if atleast one of the operand is string.
If both the operands are arithmatic then only the + operator will access as an arithmatic addition operation.
*/