Sunday, April 20, 2014

Practical 3rd - Demo of type conversion

In last post I had explained about, Type Conversion, Here I will be presenting you the demo for type conversion with explanation.

you can find below code at my Git Repository,
package in.technoscience;

1.public class Demo3 {
2. public static void main(String as[]) {
3.  byte b1 = 10;
4.  Short s1= 14;
5.  long l1= 18;
6.  int intType = 15;
7.  intType= b1; // widening 
8.  intType= s1; // widening
9. // intType= l1; // compiler unhappy (narrowing)
10.  intType=(int)l1; // narrowing from long to int
11. // float f= 30.0; // compiler unhappy
  
12.  float fv= 30.4f;
13.  float fv1=(float)30.0;
14.  //intType=fv;// compiler unhappy
15.  intType=(int) fv;
16.  System.out.println(intType); // output 30.0
17.  // same will happen in case of double also
  
18.  //byte b2 = b1+2; // compiler unhappy
19.  byte b3 = (byte) (b1 + 2);
20.  System.out.println(b1);
21.  System.out.println(b3);
  
22.  float f1 = 10.0f;
23.  float f2 = (float) (100.4 / f1);
24.  float f3 = (float)(f1-0.0);
25.  System.out.println(f1);
26.  System.out.println(f2);
27.  System.out.println(f3);

 }

}
Explanation:
At line no 7: Widening as we are assigning byte type variable to int type, so it is handled by java implicitly. no explicit cast requires for these type of conversion
At line no 8: same as line no 7, short from int
for theoretical part of type conversion follow my post Type Conversion

At line no 9: compiler is unhappy, as here we want to perform narrowing by assigning long to int but for narrowing we need to tell compiler explicitly, so we need to perform explicit cast that i have perform at line no 10.
At line no 11: In java the decimal value is considered as double by compiler.so at this line no compiler is considering that we want to convert double value to float means narrowing , so compiler complains here. To sort this problem we can use explicit cast as line no 13 or can use f with float value as i have done in line no 12.

At line no 14: here compiler is unhappy due to same narrowing reason( float to int). To get rid of that we need to use explicit cast as line no 15 and when we will print this value we will get output as 30(loss of precision according to floating point rule)

At line no 18: To understand this take a note as follow:
The result of an expression involving anything int-sized or smaller is always an int.
so at this line no we are adding two bytes together and as per rule compiler wants to assign it to int type, but we are assign the result to byte so compiler is unhappy and complains as below 
Type mismatch: cannot convert from int to byte
To get rid of this problem either we can change the type of b2 to int or we can use explicit cast like i had did at line no 19.

At line no 23 & 24: if you will see .class file using decompiler you will find
        
        float f1 = 10F;
        float f2 = (float)(100.40000000000001D / (double)f1);
        float f3 = (float)((double)f1 - 0.0D);
      
 
here compiler is first converting the denominator to double and then performing the division and then converting the result to float value.this is all done by the compiler.
Same way in next line it is converting the f1 to double and then performing the subtraction and then converting to double.

To know the theoretical part of narrowing follow my post :Widening and narrowing in java.
 

No comments:

Post a Comment