Interview Question

What is type casting?

Casting requests a permitted primitive conversion or checked reference conversion.

💡 Concept ✅ Quick Revision ☕ Java

Answer

Type casting explicitly converts a value to another permitted type. • Primitive casts perform numeric or narrowing conversions according to language rules. • A reference cast checks run-time type compatibility and may throw ClassCastException. • instanceof pattern matching can avoid unsafe manual casts.

Example

Code
Object value = "Java";
String text = (String) value;
System.out.println(text.length());
Output
4

Quick Revision

Casting requests a permitted primitive conversion or checked reference conversion.