Core Java: Type Casting And Type Conversion

Introduction 

 
When you assign a value of one type to another type, it's done fairly, but sometimes you  may get errors. 
 
For Example,
  1. public class TypeCasting {  
  2.     public static void main(String[] args) {  
  3.         byte Byte;  
  4.         Byte = 40;  
  5.         System.out.println(Byte);  
  6.     }  
  7. }  
Output
40
 
In the above example, we just assign a value to the variable Byte which is of "byte" type. As we know that a "byte" type can hold an integer "-128 to 127". The specified value 40 lies in the range. So, we just get correct output. On the other hand, look at the next example: 
  1. public class TypeCasting {  
  2.     public static void main(String[] args) {  
  3.         byte Byte;  
  4.         Byte = 40;  
  5.         Byte = Byte + 2;  
  6.         System.out.println(Byte);  
  7.     }  
  8. }  
Output
 
TypeCasting.java:5: possible loss of precision
found : int
required: byte
Byte=Byte+2;
^
1 error
 
As you can see in the example, this time you'll get an error message, eventhough you just add 2 to 40 to get 42 which also lies within the range (-128 to 127). It's a Type related issue, when you add an integer value 2 to variable Byte, the expression is (Byte + 2) automatically promoted to the "int" type and when you assign an "int" type value to a "byte" type, you get the error. To resolve such problems, you should know about the Type Casting and Conversion in Java. So, let's start. 
 

Two types of conversions exist in Java:

 

1. Implicit(Auto) Type Conversion

  1. int Integer;  
  2. long Long;  
  3. Integer = 6;  
  4. Long = Integer;  
  5. System.out.println(Long);  
In the above case, the program runs well because Java does an implicit conversion. But, consider a little change in the following given code:
  1. int Integer;  
  2. long Long;  
  3. Long = 6;  
  4. Integer=Long;  
  5. System.out.println(Integer);  
Now, you'll get an error because Java can't do an implicit conversion in this case. So, we must use a cast, which performs an Explicit Type Conversion between incompatible types. The following given table shows the compatible types for implicit conversion:
 

 
byte short int long float double char boolean
byte Y Y Y Y Y Y N N
short N Y Y Y Y Y N N
int N N Y Y Y Y N N
long N N N Y Y Y N N
float N N N N Y Y N N
double N N N N N Y N N
char N N Y Y Y Y Y N
boolean N N N N N N N Y

Implicit conversions are only possible in these conditions:
  • If two types are compitable.
  • The destination type is larger than source type.
Note: In the case of char to all int, long, float, or double conversions, you'll get the ASCII Code of the specified character.
 
Example
  1. char Char = 'a';  
  2. int Int = Char;  
  3. float Float = Char;  
  4. System.out.println(Int);  
  5. System.out.println(Float);  
Output
97
97.0 
 

2. Explicit (External) Type Conversion

 
Explicit conversion should be done place when the destination type is smaller than the source type. For example, assigning an "int" type value to "short" or "byte" type. 
 
Syntax for Explicit Type conversion
 
(target-type) value
OR
(target-type) (expression) 
 
Example
  1. byte a;  
  2. short b;  
  3. b=127;  
  4. a=(byte)b;  
  5. System.out.println(a);  
Output
127
 
Explicit Type Conversion is good only if you know the consequence; consider the above example, we assign sort type value '127' to byte type by Explicit Type Casting. Now, don't forget that 127 lies in "-128 to 127".
 
Set
            b = 128; //128 exceeds the range defined for a byte type.
 
Run the program. Programs run well but let's have a look at the output- it's a Garbage value. In such situations, you may get the undesirable results. 
 
Note: when you convert from a Floating Point Type to Integer Type explicitly, the fractional part is omitted. 
 
Type Conversions not only take place in assignments, even Type Conversions also occur in expressions automatically, which is called Type Promotion. For example,
  1. byte a,b;  
  2. int c;  
  3. a=20;  
  4. b=10;  
  5. c=a*b+b;  
  6. System.out.println(c);  
In the above example, consider the sub expression a*b (a and b are both "byte") which yields the result (200) which exceeds the range of byte operands. To get out of messes such as tat, Java automatically promotes each byte, short or char operand to int if there is an operand of "long" type in expression then the whole expression will be promoted to "long", so "float" and so "double". This is the reason for getting an error when executing code such as:
  1. short a;  
  2. a=1;  
  3. a = a * 8;  
  4. System.out.println(a); 

Conclusion

In the above article, we studied about Type Casting and Type Conversions