Type Casting in Java Interview Questions and Answers for freshers
-
What is type casting in Java?
- Answer: Type casting, also known as type conversion, is the process of converting a variable of one data type into another data type. It's essential for handling different data types in Java's strictly-typed system. There are two main types: implicit and explicit.
-
Explain implicit type casting with an example.
- Answer: Implicit type casting, or widening conversion, happens automatically when a smaller data type is assigned to a larger data type. No explicit code is needed. For example:
int x = 10; double y = x;
Here, the integerx
is automatically converted to a doubley
without any explicit cast.
- Answer: Implicit type casting, or widening conversion, happens automatically when a smaller data type is assigned to a larger data type. No explicit code is needed. For example:
-
Explain explicit type casting with an example.
- Answer: Explicit type casting, or narrowing conversion, requires a manual cast using parentheses. It's necessary when converting a larger data type to a smaller one, potentially leading to data loss. Example:
double z = 10.5; int a = (int) z;
Here, the doublez
is explicitly cast to an integera
, truncating the decimal part.
- Answer: Explicit type casting, or narrowing conversion, requires a manual cast using parentheses. It's necessary when converting a larger data type to a smaller one, potentially leading to data loss. Example:
-
What are the possible data loss scenarios in type casting?
- Answer: Data loss can occur during narrowing conversions. For example, converting a double to an int truncates the fractional part. Converting a larger integer to a smaller integer (like a long to an int) can lead to overflow if the value exceeds the smaller type's range.
-
What happens if you try to cast a String to an int without proper parsing?
- Answer: You'll get a compile-time error. Direct casting from String to a numeric type like int is not allowed. You need to use methods like
Integer.parseInt()
or similar to parse the String into an integer first.
- Answer: You'll get a compile-time error. Direct casting from String to a numeric type like int is not allowed. You need to use methods like
-
How do you cast a String to an integer in Java?
- Answer: Use
Integer.parseInt("yourString")
. This method attempts to convert the String to an int. It throws aNumberFormatException
if the String cannot be parsed.
- Answer: Use
-
What is the difference between casting and converting?
- Answer: In Java, the terms are often used interchangeably, especially in the context of primitive data types. However, a subtle difference is that "converting" might encompass broader data transformations (like converting a String to a Date), while "casting" is more specifically about changing the type of a variable.
-
Can you cast an object of a subclass to its superclass?
- Answer: Yes, this is called upcasting and is generally safe. It's implicitly allowed because a subclass "is-a" superclass. The subclass object will retain its original type, but the reference will be treated as if it's of the superclass type.
-
Can you cast an object of a superclass to its subclass?
- Answer: Yes, but this is called downcasting and requires an explicit cast. It's potentially unsafe and can lead to a
ClassCastException
at runtime if the object isn't actually an instance of the subclass.
- Answer: Yes, but this is called downcasting and requires an explicit cast. It's potentially unsafe and can lead to a
Thank you for reading our blog post on 'Type Casting in Java Interview Questions and Answers for freshers'.We hope you found it informative and useful.Stay tuned for more insightful content!