Type Casting in Java Interview Questions and Answers for 2 years experience
-
What is type casting in Java?
- Answer: Type casting, also known as type conversion, is the process of converting a variable from one data type to another. Java supports two types: implicit (automatic) and explicit (manual).
-
Explain implicit type casting with an example.
- Answer: Implicit casting happens automatically when a smaller data type is assigned to a larger data type without any explicit conversion. For example, assigning an `int` to a `double`:
double d = 10;
. The `int` value 10 is automatically widened to a `double` value 10.0.
- Answer: Implicit casting happens automatically when a smaller data type is assigned to a larger data type without any explicit conversion. For example, assigning an `int` to a `double`:
-
Explain explicit type casting with an example.
- Answer: Explicit casting, also known as narrowing conversion, requires manual intervention using casting operators (e.g., `(int)`, `(double)`). It's used when converting a larger data type to a smaller one. This can lead to data loss if the value doesn't fit within the smaller type. Example:
int i = (int) 10.5;
(10.5 is converted to 10, the fractional part is truncated).
- Answer: Explicit casting, also known as narrowing conversion, requires manual intervention using casting operators (e.g., `(int)`, `(double)`). It's used when converting a larger data type to a smaller one. This can lead to data loss if the value doesn't fit within the smaller type. Example:
-
What are the potential risks of explicit type casting?
- Answer: The main risk is data loss. When narrowing, information might be lost (like the fractional part in the previous example). It can also lead to exceptions like `ClassCastException` if you're casting between incompatible object types.
-
What is the difference between widening and narrowing type casting?
- Answer: Widening casting is the automatic conversion from a smaller data type to a larger one (e.g., `int` to `long`), generally safe. Narrowing casting is the explicit conversion from a larger data type to a smaller one (e.g., `double` to `int`), potentially leading to data loss or exceptions.
-
Can you cast a String to an Integer in Java? How?
- Answer: You can't directly cast a String to an Integer. You need to use methods like `Integer.parseInt()` or `Integer.valueOf()`. For example: `int num = Integer.parseInt("123");` or `Integer num = Integer.valueOf("123");`.
-
What happens if you try to cast a value outside the range of the target data type?
- Answer: You'll get unexpected results. For numerical types, the value might wrap around (for example, with `byte` which has a limited range) or be truncated (e.g., losing the fractional part when casting a `double` to an `int`).
-
Explain the concept of upcasting and downcasting in the context of inheritance.
- Answer: Upcasting is automatically converting a subclass object to its superclass type. It's safe and usually implicit. Downcasting is explicitly converting a superclass object to a subclass type. It requires an explicit cast and can throw a `ClassCastException` if the object isn't actually an instance of the target subclass.
-
What is the `instanceof` operator used for?
- Answer: The `instanceof` operator checks if an object is an instance of a particular class or interface. It's often used before downcasting to prevent `ClassCastException` exceptions.
Thank you for reading our blog post on 'Type Casting in Java Interview Questions and Answers for 2 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!