Type Casting - Notes by Shariq SP
Understanding Type Casting in Java
Type casting is the process of converting a variable of one data type into another data type. In Java, type casting can be classified into two types: primitive type casting and reference type casting.
Primitive Type Casting
Primitive type casting involves converting between primitive data types, such as int, double, float, etc. It can be further categorized into widening (implicit) casting and narrowing (explicit) casting. Widening casting occurs when converting a smaller data type to a larger data type, while narrowing casting occurs when converting a larger data type to a smaller data type.
Widening (Implicit) Casting
Widening casting occurs automatically when converting a smaller data type to a larger data type. It is achieved when:
int numInt = 10;
double numDouble = numInt; // Widening casting (int to double)
Narrowing (Explicit) Casting
Narrowing casting involves converting a larger data type to a smaller data type. It must be performed explicitly by the programmer using parentheses and may result in data loss if the value being casted is outside the range of the target data type. It is achieved when:
double numDouble = 10.5;
int numInt = (int) numDouble; // Narrowing casting (double to int)
Reference Type Casting
Reference type casting involves converting between reference types, such as objects and their subclasses or interfaces. It is achieved through inheritance and interfaces in Java.
Upcasting (Implicit Reference Type Casting)
Upcasting occurs when a superclass reference variable is used to refer to a subclass object. It is implicit and is achieved when:
class Animal { }
class Dog extends Animal { }
Animal animal = new Dog(); // Upcasting (Dog to Animal)
Downcasting (Explicit Reference Type Casting)
Downcasting involves explicitly specifying the target reference type using parentheses. It is achieved when:
Animal animal = new Dog();
Dog dog = (Dog) animal; // Downcasting (Animal to Dog)
Rules of Downcasting
When performing downcasting in Java, the following rules should be followed:
- Downcasting can only be performed if the object being referred to is an instance of the target subclass.
- If the object being referred to is not an instance of the target subclass, a
ClassCastException
will be thrown at runtime. - Downcasting should be used cautiously and only when necessary, as it may lead to runtime errors if not handled properly.
Important Points to Remember
- Primitive type casting is used to convert between primitive data types.
- Reference type casting is used to convert between reference types, such as objects and their subclasses or interfaces.
- Widening casting occurs automatically, while narrowing casting must be performed explicitly.
- Upcasting is implicit and allows a superclass reference to refer to a subclass object.
- Downcasting is explicit and involves specifying the target subclass type.
Type Casting Interview Questions
- What is type casting in Java?
- Explain widening and narrowing casting with examples.
- What are the types of type casting in Java?
- How is implicit type casting achieved in primitive type casting?
- When is explicit type casting required in primitive type casting?
- What is upcasting in reference type casting?
- Explain downcasting in reference type casting with rules.
- What happens if downcasting is performed on an object that is not an instance of the target subclass?
- When should you use downcasting in Java?
- What is the ClassCastException and when does it occur?
- What are the risks associated with narrowing casting in primitive type casting?
- Can you perform implicit casting from double to int?
- How can you convert an int to a String in Java?
- Explain the role of parentheses in explicit type casting.
- When is implicit casting not allowed?
- What are the benefits of upcasting and downcasting?
- What is the difference between implicit and explicit type casting?
- How can you avoid ClassCastException when performing downcasting?
- When does widening casting occur in Java?
- What is the purpose of type casting in Java?
Multiple Choice Questions (MCQs)
- Which type of casting occurs automatically in Java?
- Widening casting
- Narrowing casting
- Upcasting
- Downcasting
- What is the result of casting a double to an int?
- The integer part of the double
- The fractional part of the double
- An error
- A warning
- Which casting is considered safer in Java?
- Implicit casting
- Explicit casting
- Widening casting
- Narrowing casting
- What happens if downcasting is performed on an object that is not an instance of the target subclass?
- Compilation error
- Runtime exception
- No effect
- Implicit casting
- Which casting should be used cautiously due to the risk of data loss?
- Implicit casting
- Explicit casting
- Widening casting
- Narrowing casting
- What is the syntax for explicit type casting in Java?
int x = (int) 10.5;
int x = (int) "10";
double y = (double) 5;
float z = (float) 10.0;
- What happens if you attempt to cast a String to an int?
- It results in a compilation error.
- The String is converted to an int.
- It results in a runtime exception.
- It results in a warning.
- When does implicit casting not occur in Java?
- When casting between numeric types
- When casting between object types
- When casting between boolean types
- Implicit casting occurs in all cases
- Which casting is used to convert a superclass reference to a subclass reference?
- Upcasting
- Downcasting
- Widening casting
- Narrowing casting
- What is the primary purpose of type casting in Java?
- To convert between different data types
- To compare objects
- To perform arithmetic operations
- To create new objects