0% found this document useful (0 votes)
14 views2 pages

Java Casting Interview Questions Guide

The document explains casting in Java, which is the process of converting one type of information into another. It details two main types of casting: Primitive Casting (including implicit, explicit, and boolean casting) and Non-Primitive Casting (including up casting and down casting). Each type is further defined with examples illustrating how data types can be converted and the implications of these conversions.

Uploaded by

patilrohini0224
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Java Casting Interview Questions Guide

The document explains casting in Java, which is the process of converting one type of information into another. It details two main types of casting: Primitive Casting (including implicit, explicit, and boolean casting) and Non-Primitive Casting (including up casting and down casting). Each type is further defined with examples illustrating how data types can be converted and the implications of these conversions.

Uploaded by

patilrohini0224
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Casting in JAVA interview Questions

1. What is casting?
Converting one type of information into another type is called “casting”.
Type casting is nothing but changing the type of the data. Using type
casting, only type of the data can be changed but not the data itself.
In Java, casting is classified into two types:
i. Primitive Casting
ii. Non-Primitive Casting.

2. What are the types of casting?


There are two types of casting:
i. Primitive Casting
ii. Non-Primitive Casting

Primitive Casting:

Converting one data type of information into another data type is called
primitive casting.

Primitive casting is classified into three types:

a. Implicit casting- converting lower data type information into higher data
type information is called implicit casting.
Implicit casting is also called widening casting, where memory size goes
on increasing.
Ex. int a = 5; //memory size of int is 4byte
Sop(a); //5
double b=a; //memory size of double is 8byte
Sop(b); //5.0

b. Explicit casting – converting higher data type information into lower data
type information is called explicit casting.
Explicit casting is also called as narrowing casting, where memory size
goes on decreasing.
In explicit casting data loss takes place.
Ex. double b = 2.5; // memory size of double is 8byte
Sop(b); //2.5
Int a = (int)b; // memory size of int is 4byte
Sop(a); //2
c. Boolean casting – Boolean casting is considered to be incompatible
casting type, because Boolean data type is unique type of data type.
When information is already predeclared inside it.
Ex. boolean str = true;

Non-Primitive Casting:

Converting one type of class into another type of class is called non-primitive
casting.

Non-primitive is classified into two types:

a. Up casting
b. Down casting.

Up Casting-

Assigning subclass property into super class is called up casting.

Before performing up casting first we need to perform inheritance operation.

After performing inheritance, the property which is present inside super class
comes into sub class.

In the sub class programmer can declare new properties.

At the time of up casting the properties which are inherited from super class are
only eligible for the up casting operation.

The new property which was declared inside subclass is not eligible for up
casting operation.

Down Casting-

Assigning super class property into sub class is called down casting.

Before performing down casting first we need to perform up casting.

You might also like