Java type conversion and casting

Introduction

Type conversion and casting happens when assigning a value of one type to a variable of another type.

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

For example, it is always possible to assign an int value to a long variable.

There is no automatic conversion defined from double to byte.

To do a conversion between incompatible types, use a cast.

Casting performs an explicit conversion between incompatible types.

Java Automatic Conversions

When one type of data is assigned to another type of variable, an automatic type conversion occurs if the following two conditions are met:

  • The two types are compatible.
  • The destination type is larger than the source type.

When these two conditions are met, a widening conversion takes place.

For example, the int type is always large enough to hold all valid byte values, so no explicit cast statement is required.

The numeric types, including integer and floating-point types, are compatible with each other during widening conversions.

However, there are no automatic conversions from the numeric types to char or boolean.

char and boolean are not compatible with each other.

Java performs an automatic type conversion when storing a literal integer constant into variables of type byte, short, long, or char.

Casting Incompatible Types

To create a conversion between two incompatible types, use a cast.

A cast is an explicit type conversion.

It has this general form:

(target- type) value 

Here, target-type specifies the desired type to convert the specified value to.

For example, the following code casts an int to a byte.

int a; 
byte b;  

b = (byte) a; 

Truncation

When a floating-point value is assigned to an integer type, the truncation conversion happens.

Java integers do not have fractional components.

Thus, when a floating-point value is assigned to an integer type, the fractional component is lost.

For example, if the value 1.23 is assigned to an integer, the resulting value will simply be 1.

If the size of the whole number component is too large to fit into the target integer type, the value will be reduced to the target type's range.

The following program demonstrates some type conversions that require casts:

// Demonstrate casts.  
public class Main{  
  public static void main(String args[]) {  
    byte b;  /* ww  w.j a  v a2s .c o  m*/
    int i = 500;  
    double d = 123.142;  
  
    System.out.println("\nConversion of int to byte.");  
    b = (byte) i;  
    System.out.println("i and b " + i + " " + b);  
  
    System.out.println("\nConversion of double to int.");  
    i = (int) d;  
    System.out.println("d and i " + d + " " + i);  
  
    System.out.println("\nConversion of double to byte.");  
    b = (byte) d;  
    System.out.println("d and b " + d + " " + b);  
  }  
} 

Automatic Type Promotion in Expressions

For example, examine the following expression:

byte a = 40;  
byte b = 50;  
byte c = 100;  
int d = a * b / c; 

The result of the intermediate term ab* exceeds the range of either of its byte operands.

Java automatically promotes each byte, short, or char operand to int when evaluating an expression.

This means that the expression ab* is performed using integers-not bytes.

For example, this seemingly correct code causes a problem:

byte b = 50;  
b = b * 2; // Error! Cannot assign an int to a byte! 

The code is trying to store 50 * 2, a perfectly valid byte value, back into a byte variable.

Since the operands were automatically promoted to int when the expression was evaluated, the result has been promoted to int.

Thus, the result of the expression is now of type int, which cannot be assigned to a byte without the use of a cast.

This is true if the value being assigned would still fit in the target type.

We have to use an explicit cast to make it work, such as

byte b = 50;  
b = (byte)(b * 2); 



PreviousNext

Related