Type Casting in Java Interview Questions and Answers for 10 years experience

100 Java Type Casting Interview Questions & Answers (10+ Years Experience)
  1. What is type casting in Java?

    • Answer: Type casting, also known as type conversion, is the process of changing an object from one data type to another. In Java, this is crucial because it's a strongly-typed language, meaning variables must have a declared type. Type casting allows for flexibility when dealing with different data types, but it must be done carefully to avoid errors.
  2. Explain the difference between implicit and explicit type casting.

    • Answer: Implicit casting (also known as automatic type conversion) happens automatically when the compiler can safely convert a smaller data type to a larger one without data loss (e.g., `int` to `double`). Explicit casting (also known as manual type conversion) requires the programmer to explicitly specify the conversion using casting operators (`(dataType)`) because there's a potential for data loss or other issues (e.g., `double` to `int`).
  3. What happens when you cast a `double` to an `int`? Illustrate with an example.

    • Answer: When casting a `double` to an `int`, the fractional part is truncated (not rounded). The value is simply dropped. For example, `(int) 3.9` will result in `3`. ` (int) -2.7` will result in `-2`.
  4. Can you cast a `String` to an `int` directly? If not, how would you do it?

    • Answer: No, you cannot directly cast a `String` to an `int`. You need to use the `Integer.parseInt()` method (or similar methods for other numeric types like `Double.parseDouble()`). For example: `int num = Integer.parseInt("123");`
  5. What is the significance of the `instanceof` operator in relation to type casting?

    • Answer: The `instanceof` operator checks if an object is an instance of a particular class or interface. This is crucial before performing casting, particularly when dealing with polymorphism and inheritance to prevent `ClassCastException`s. You should use `instanceof` to verify the type before downcasting.
  6. Explain narrowing and widening type casting with examples.

    • Answer: Widening casting converts a smaller data type to a larger one (e.g., `int` to `long`, `float` to `double`). This is usually implicit and safe. Narrowing casting converts a larger data type to a smaller one (e.g., `double` to `int`, `long` to `int`). This requires explicit casting and can lead to data loss.
  7. Describe the potential pitfalls of type casting.

    • Answer: The main pitfall is the `ClassCastException`, which occurs when you try to cast an object to a type it's not an instance of. Data loss is another risk during narrowing conversions. Incorrect casting can lead to unexpected behavior and program crashes.
  8. How does type casting work with inheritance in Java?

    • Answer: When dealing with inheritance, you can cast a subclass object to its superclass (upcasting – often implicit), but casting a superclass object to a subclass (downcasting) requires explicit casting and an `instanceof` check to avoid `ClassCastException`s. Upcasting is generally safe; downcasting is not.

Thank you for reading our blog post on 'Type Casting in Java Interview Questions and Answers for 10 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!