Java Type Conversion and Casting - The Coding Shala

Home >> Learn Java >> Java Type Conversion and Casting

Type Conversion and Casting

What do you mean by type conversion? Type conversion is to assign a value of one type to a variable of another type. for example - 

int a = 10;
long b = a;

If two types are compatible then Java will perform the conversion automatically and we have to use a cast, for explicit conversion between incompatible types. 
Java Type Conversion and Casting - The Coding Shala

Java's Automatic Conversions


In Java, when we assign one type of data to another type of variable an automatic conversion will happen only if both types are compatible and the destination type is larger than the source type. For example byte, to int, an automatic conversion will happen. This type of conversion is also called a widening conversion.



Java Casting


In Java, for the incompatible type, we have to do the casting. For example, if we want to assign an int value to a byte variable an automatic conversion will not take place because the byte type is smaller than int. This type of conversion is also called a narrowing conversion. A cast is explicit type conversion. The basic syntax is as follows - 

type identifier = (target- type) value;

Here target_type has specified the type that we have to convert into. The following code shows how to convert int to byte - 

int a;
byte b;

b = (byte) a;

Note:- If the integer's value is larger than the range of a byte, it will be reduced modulo( the remainder of an integer division by the ) byte's range. This will happen for every type if the range is more than the desired type.

When a floating-point value is assigned to an integer then the fractional component is lost. For example, if the value 2.25 is assigned to an integer then the resulting value will be 2. 

The following Java program demonstrates type conversions - 
class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
long l;
double d = 323.142;
System.out.println("\nConversion of int to long.");
l = i; //automatic conversion
System.out.println("i and l " + i + " " + l);
System.out.println("\nConversion of int to byte.");
b = (byte) i; //casting
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);
}
}



Output>>>>>>

Conversion of int to long.
i and l 257 257

Conversion of int to byte.
i and b 257 1

Conversion of double to int.
d and i 323.142 323

Conversion of double to byte.
d and b 323.142 67

Java Automatic Type Promotion

In Java, automatic type promotion will happen in expressions where the intermediate value will sometimes exceed the range of either operand. Java automatically promotes each byte, short, or char to int when evaluating an expression. For example, consider the below example - 

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

Here, a * b easily exceeds the range of byte operands so a*b is performed using integers not bytes. 

In Java, all byte, short, and char values are promoted to int. If one operand is long the whole expression is promoted to long. If one operand is a float then the entire expression is promoted to float. If any operand is double then the result is double.

NOTE: if we use " "(double quotes), the text is treated as a string and when we use ' '(single quotes), the characters are converted to int. This is called widening primitive conversion.

Point to Remember: Widening primitive conversion happens only when an operator like '+' is present which expects at least integer on both sides.



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Java Program to Convert Binary to Decimal - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Java Program to Convert Decimal to Binary - The Coding Shala