Google+

47. Type conversion






It is fairly common to assign a value of one type to a variable of another type.


If the two types are compatible, then Java will perform the conversion automatically.

Program to demonstrate that Java will perform the conversion automatically when the value and variables types are different but compatible.

class LongVariableIntegerValue
{
   public static void main(String args[])
   {
         long a=123;    // We have assigned an integer value to a variable which is declared as long knowing that the both types are different but compatible
         System.out.println("a="+a);
   }
 }

Output of this program:

a=123

In this program, Java performed the conversion automatically though the data types of the variable (i.e. long) and value (i.e. int) are different but compatible.



However, not all the types are compatible and thus Java can't perform conversion automatically for incompatible types.

Program to demonstrate that Java wont perform the conversion automatically when the value and variables types are not compatible.

class IntVariableDecimalValue
{
   public static void main(String args[])
   {
         int a=123.456;    // We have assigned a decimal value to a variable which is declared as int knowing that both types are not compatible
         System.out.println("a="+a);
   }
}

Output of this program:

Error "Possible loss of precision int a=123.456"

In this program, Java didn't perform the conversion automatically as the data types of the value and variable are not compatible



Fortunately it is still possible to obtain a conversion between the incompatible types using cast which performs an explicit conversion between incompatible types.

Program to demonstrate the usage of cast in converting the integer value to byte value while assigning the integer value to a byte variable. 

class CastByteVariableIntValue
{
   public static void main(String args[])
   {
         byte a = (byte)12345;  // We've used (byte) cast for converting the integer value '12345' in to byte format before storing in the byte variable 'a'

         System.out.println("a="+a);
   }
}

Output of the program:

a=57 

Observer that (byte) cast in this program converted the integer value 12345 into a byte value 57.



Program to demonstrate the usage of cast in converting the decimal value to int value while assigning the decimal value to an int variable. 

class CastIntVariableDecimalValue
{
   public static void main(String args[])
   {

         int a=(int) 323.142;   // We've used (int) cast for converting the decimal value '323.142' in to int format before storing in the int variable 'a'

         System.out.println("a="+a);

   }
}

Output of the program:

a=323

Observer that (int) cast in this program converted the decimal value 323.142 into an int value 323.



Program to demonstrate that Values in the expression gets promoted to match the second argument to each binary operator.

 class PromotedType
{
   public static void main(String args[])
   {

         int a=5;             // a is of int type
         double b=0.55;  // b is of double type

         System.out.println(a+b);  // The result of a+b expression will get promoted to double as one of the variable i.e. 'b' has type double and other variable 'a' has type int 

   }
}

Output of this program:

5.55 

Observe that the result is promoted to double type, when the variable 'a' is integer and variable 'b' is double.






Please comment below to feedback or ask questions.

Arrays concept will be explained in the next post.




4 comments:

vathsa said...

In the different data type conversion like int a=10 and double d=0.21 Then a+d = 100.21 not 10.21.Please correct it in the output mentioned above.

Arun Motoori said...

@vastha - The correct output is 10.21. Please double check.

PREETI ARUN said...

For below pgm I am getting o/p as " a+b:100.21 " where as o/p should be a+b:10.21

class PromotedType
{
public static void main(String args[])
{

int a=10 ; // a is on int type
double b=0.21; // b is of double type

System.out.println("a+b:" +a+b);

}
}

Where as for below pgm I am getitng o/p as a+b:10.21 , i think its because I have kept the operation a+b inside the bracket.

class PromotedType
{
public static void main(String args[])
{

int a=10 ; // a is on int type
double b=0.21; // b is of double type

System.out.println("a+b:"+(a+b));
}
}

Arun Motoori said...

@Preeti - You are right.

In the first case -> System.out.println("a+b:" +a+b);

+ is acting as a string concatenation operator i.e. "10" string text is concatenated with "0.21" string text. Hence 100.21 is printed.

In the second case -> System.out.println("a+b:" +(a+b));

+ is acting as an addition operator i.e. 10+0.21= 10.21

Blog Index