ARTICLE

Core Java: Type Casting And Conversion

Posted by Marcus Articles | Java July 04, 2011
Type casting and conversion is a basic concept in core Java that Java newcomers should consider to be as important as having breakfast. This article demonstrates how type conversions take place in Java.
Reader Level:

When you assign a value of one type to another type, it's done fairly, but sometimes you  may get errors.
For Example,

public class TypeCasting {
public static void main(String[] args) {
byte Byte;
Byte=40;
System.out.println(Byte);
}
}

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:
public class TypeCasting {
public static void main(String[] args) {
byte Byte;
Byte=40;
Byte=Byte+2;
System.out.println(Byte);
}
}

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
Example:
int Integer;
long Long;
Integer = 6;
Long = Integer;
System.out.println(Long);

In the above case, the program runs well because Java does implicit conversion. But, consider a little change in the following given code:
int Integer;
long Long;
Long = 6;
Integer=Long;
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:
char Char = 'a';
int Int = Char;
float Float = Char;
System.out.println(Int);
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:
byte a;
short b;
b=127;
a=(byte)b;
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,
byte a,b;
int c;
a=20;
b=10;
c=a*b+b;
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:
short a;
a=1;
a = a * 8;
System.out.println(a);

COMMENT USING