Java Introduction
Getting StartedÂ
Java Syntax
Java Output – Printing Text
Java Output: Printing Numbers
Java Comments
Java Variables
Java Print Variables
Java: Declare Multiple Variables
Java Identifiers
Real-Life Examples
Java Type Casting
Java Data Types
Java Numbers
Java Boolean
Java Characters and Strings
Real-Life Example
Advanced Billing Example
Non-Primitive Data Types in Java
Type Casting
….
Type Casting – Converting Data Types Like a Pro
In Java, type casting allows you to convert a value from one data type to another. This is essential when you want different types to work together in calculations or when optimizing memory usage.
Java offers two main types of type casting for primitive data types:
1. Widening Casting (Implicit Casting)
Widening casting happens automatically when a smaller data type is converted into a larger one.
This is safe because a larger data type can store all possible values of a smaller type without losing information.
Order of widening:
byte → short → char → int → long → float → double
Example – Widening Casting in Action
public class WideningExample {
public static void main(String[] args) {
int items = 42;
double totalItems = items; // Implicit conversion
System.out.println("Integer value: " + items);
System.out.println("Double value after casting: " + totalItems);
}
}
Output:
Integer value: 42
Double value after casting: 42.0
2. Narrowing Casting (Explicit Casting)
Narrowing casting must be done manually because you might lose data in the process.
You need to place the desired type in parentheses before the value.
Order of narrowing:
double → float → long → int → char → short → byte
Example – Narrowing Casting in Action
public class NarrowingExample {
public static void main(String[] args) {
double price = 99.99d;
int roundedPrice = (int) price; // Explicit conversion
System.out.println("Original price: " + price);
System.out.println("Rounded price: " + roundedPrice);
}
}
Output:
Original price: 99.99
Rounded price: 99
Real-Life Example – Currency Converter
In this example, we use type casting to convert between double
and int
when calculating how many whole units of a currency you can get.
public class CurrencyConverter {
public static void main(String[] args) {
double dollarAmount = 154.75;
double exchangeRate = 82.45; // 1 USD = 82.45 INR
double inrAmount = dollarAmount * exchangeRate;
int wholeRupees = (int) inrAmount; // Narrowing casting
System.out.println("Dollar amount: $" + dollarAmount);
System.out.println("Amount in INR: ₹" + inrAmount);
System.out.println("Whole rupees only: ₹" + wholeRupees);
}
}
Output:
Dollar amount: $154.75
Amount in INR: ₹12754.8875
Whole rupees only: ₹12754
Why Type Casting Matters in Java
- Accuracy – Ensures calculations are performed with the correct level of precision.
- Compatibility – Allows different numeric types to work together.
- Memory Management – Convert to smaller types when you want to optimize memory.
💡 Pro Tip: Always double-check before narrowing casting, as it can cause data loss or unexpected results if the value doesn’t fit in the target type.