Java Type Casting

Type Casting in Java: Automatic and Explicit Conversions

In Java, type casting allows you to assign a value of one data type to a variable of another data type. It’s a powerful feature that makes your code more flexible—but only if you use it correctly.

In this post, you’ll learn:

  • What type casting is

  • The difference between automatic and explicit type conversion

  • A working Java program with examples and output

What Is Type Casting?

Type casting is the process of converting a value from one data type to another. There are two types of type casting which are:

1) Automatic (Implicit) Type Conversion

Java automatically performs type conversion when:

  1. The two data types are compatible (e.g., int to long)

  2. The destination type is larger than the source type

Example:

int x = 10;

long y = x; // Implicit casting from int to long


2) Explicit Type Conversion (Casting)

When the data types are incompatible or the conversion is from a larger type to a smaller one, you need to perform an explicit type cast using this syntax:

(target-type) value

Example:

int x = 100;

byte y = (byte) x; // Explicit casting from int to byte

Example: Java Program Demonstrating Type Casting

PROGRAM
class TypeCasting {

 public static void main(String[] args) {
  
  byte b;
  int val = 263;
  double d = 9563.25;
  long l = 56322145;
  
  System.out.println("int val = "+val);
  System.out.println("double d = "+d);
  System.out.println("long l = "+l);
  
  System.out.println("\nint to byte ");
  b = (byte) val;
  System.out.println(val+" to "+b);
  
  System.out.println("\ndouble to int ");
  System.out.println(d+" to "+(int)d);
  
  System.out.println("\nlong to double ");
  System.out.println(l+" to "+(double)l);
  
  System.out.println("\nlong to short ");
  System.out.println(l+" to "+(short)l);
  
  System.out.println("\ndouble to byte ");
  System.out.println(d+" to "+(byte)d);
 }
}
OUTPUT
C:\>javac TypeCasting.java
C:\>java TypeCasting
int val = 263
double d = 9563.25
long l = 56322145

int to byte 
263 to 7

double to int 
9563.25 to 9563

long to double 
56322145 to 5.6322145E7

long to short 
56322145 to 26721

double to byte 
9563.25 to 91

What's Happening in the Output?

263 to 7: The value 263 is too large for a byte (-128 to 127), so it wraps around.

9563.25 to 9563: The decimal part is truncated when casting double to int.

56322145 to 5.6322145E7: Casting long to double results in scientific notation.

56322145 to 26721: short has a smaller range (-32,768 to 32,767), causing overflow.

9563.25 to 91: double to byte results in both truncation and overflow.

Key Takeaways:

  • Use automatic casting when you're converting to a larger or compatible type.
  • Use explicit casting when converting to a smaller or incompatible type.
  • Always be cautious of data loss or overflow, especially with smaller types like byte or short.

Practice Tip:

Try modifying the program to convert:
  • float to int
  • char to int
  • int to char
This will help you get a better grasp of how casting behaves across different data types.

Comments

Popular posts from this blog

Develop an Android application to display “Hello World!” on screen

Program to design an applet which draws a circle (having color BLUE) inside a triangle (having color YELLOW)

Build a Simple Android App to Display Student Details