Answer
Type Casting in Java
Primitive Type Casting
Java
public class TypeCasting {
public static void main(String[] args) {
// ─── WIDENING (Implicit/Automatic) ───
// byte → short → int → long → float → double
byte b = 10;
short s = b; // byte → short (automatic)
int i = s; // short → int (automatic)
long l = i; // int → long (automatic)
float f = l; // long → float (automatic)
double d = f; // float → double (automatic)
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("int: " + i);
System.out.println("long: " + l);
System.out.println("float: " + f);
System.out.println("double: " + d);
// ─── NARROWING (Explicit/Manual) ───
// May lose data!
double pi = 3.14159;
int piInt = (int) pi; // explicit cast — truncates decimal
byte smallByte = (byte) 300; // 300 > 127 (byte max) → overflow: 44
System.out.println("\ndouble to int: " + pi + " → " + piInt); // 3
System.out.println("300 to byte: 300 → " + smallByte); // 44
}
}
Output
CODE
byte: 10
short: 10
int: 10
long: 10
float: 10.0
double: 10.0
double to int: 3.14159 → 3
300 to byte: 300 → 44
Common Conversions for Automation
Java
// String → int (parse)
String strNum = "42";
int intNum = Integer.parseInt(strNum); // 42
// int → String
String fromInt = String.valueOf(42); // "42"
String fromInt2 = Integer.toString(42); // "42"
// String → double
double price = Double.parseDouble("29.99"); // 29.99
// double → int (truncate)
int priceInt = (int) price; // 29
// char → int (ASCII value)
char c = 'A';
int ascii = (int) c; // 65
// int → char
char fromInt3 = (char) 65; // 'A'
Object Casting — Upcasting and Downcasting
Java
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Woof"); }
void fetch() { System.out.println("Fetching ball!"); }
}
// ─── UPCASTING (Implicit) ───
// Child → Parent reference (automatic, safe)
Animal animal = new Dog(); // Dog IS-A Animal
animal.sound(); // "Woof" (runtime polymorphism)
// animal.fetch(); // ERROR: Animal reference can't call Dog methods
// ─── DOWNCASTING (Explicit) ───
// Parent → Child reference (must be explicit, may fail)
if (animal instanceof Dog) { // always check with instanceof first!
Dog dog = (Dog) animal; // safe downcast
dog.fetch(); // "Fetching ball!" — Dog method accessible now
}
// Unsafe downcast — causes ClassCastException
Animal cat = new Animal();
// Dog dog2 = (Dog) cat; // ClassCastException at runtime!
instanceof Operator
Java
Object obj = "Hello";
System.out.println(obj instanceof String); // true
System.out.println(obj instanceof Integer); // false
System.out.println(obj instanceof Object); // true (everything is Object)
// Java 16+ Pattern Matching instanceof
if (obj instanceof String s) {
System.out.println("Length: " + s.length()); // no explicit cast needed
}
Data Loss Table
| From | To | Loss? |
|---|---|---|
int (100) | byte | Possible (byte max=127) |
double (3.14) | int | Yes — decimal lost → 3 |
long (10L) | int | Possible (if > Integer.MAX_VALUE) |
int | double | No |
int | long | No |
