Java Type Casting

Converting between different data types in Java

🔄 What is Type Casting?

Type casting is converting a variable from one data type to another. Java supports automatic conversion (widening) and manual conversion (narrowing) between compatible data types.


// Automatic casting (widening)
int number = 100;
double decimal = number;  // int to double

// Manual casting (narrowing)
double price = 19.99;
int dollars = (int) price;  // double to int
                                    

Output:

number: 100

decimal: 100.0

price: 19.99

dollars: 19

Types of Type Casting

⬆️

Widening Casting

Automatic conversion to larger type

int num = 50;
double result = num; // Automatic
⬇️

Narrowing Casting

Manual conversion to smaller type

double num = 50.7;
int result = (int) num; // Manual
🔤

String Conversion

Convert numbers to strings

int age = 25;
String ageText = String.valueOf(age);
🔢

Parse Methods

Convert strings to numbers

String text = "123";
int number = Integer.parseInt(text);

🔹 Widening Casting (Automatic)

Java automatically converts smaller data types to larger ones:

// Widening casting examples
byte smallNum = 10;
short mediumNum = smallNum;  // byte to short
int regularNum = mediumNum;  // short to int
long bigNum = regularNum;    // int to long
float floatNum = bigNum;     // long to float
double doubleNum = floatNum; // float to double

System.out.println("Original byte: " + smallNum);
System.out.println("As double: " + doubleNum);

Output:

Original byte: 10

As double: 10.0

🔹 Narrowing Casting (Manual)

You must manually cast when converting to smaller data types:

// Narrowing casting examples
double originalValue = 123.456;
float floatValue = (float) originalValue;
long longValue = (long) originalValue;
int intValue = (int) originalValue;
short shortValue = (short) originalValue;
byte byteValue = (byte) originalValue;

System.out.println("Original: " + originalValue);
System.out.println("As int: " + intValue);
System.out.println("As byte: " + byteValue);

Output:

Original: 123.456

As int: 123

As byte: 123

🔹 String Conversions

Converting between strings and numbers:

// Number to String
int age = 25;
double price = 19.99;
boolean isActive = true;

String ageText = String.valueOf(age);
String priceText = String.valueOf(price);
String statusText = String.valueOf(isActive);

// String to Number
String numberText = "42";
String decimalText = "3.14";

int parsedInt = Integer.parseInt(numberText);
double parsedDouble = Double.parseDouble(decimalText);

System.out.println("Age as text: " + ageText);
System.out.println("Parsed number: " + parsedInt);

Output:

Age as text: 25

Parsed number: 42

🔹 Common Casting Scenarios

Practical examples of type casting:

// Calculating average (int division problem)
int total = 100;
int count = 3;
double average = (double) total / count;  // Cast to get decimal result

// Getting whole dollars from price
double productPrice = 29.99;
int wholeDollars = (int) productPrice;

// Converting user input
String userInput = "25";
int userAge = Integer.parseInt(userInput);

System.out.println("Average: " + average);
System.out.println("Whole dollars: " + wholeDollars);
System.out.println("User age: " + userAge);

Output:

Average: 33.333333333333336

Whole dollars: 29

User age: 25

Important Notes:

  • Narrowing casting may lose precision or data
  • Always cast when going from larger to smaller types
  • Use parse methods carefully - they can throw exceptions
  • Widening casting is always safe

🧠 Test Your Knowledge

Which casting happens automatically in Java?