Help the world stop coronavirus! Stay home!

Prev Next

Java / Casting

Could not find what you were looking for? send us the question and we would be happy to answer your question.

Is Dynamic casting available in Java?
for e.g. typeVariable = (Dynamic-Type) list.get(0);

No.not possible.

What is type casting in Java?

The conversion of data from one data type to another data type is termed as type casting. Type casting refers to changing the type of the data.

Note that using type casting, only type of the data can be changed not the data itself.

Explain the different types of casting in Java.

There are 2 types.

Primitive casting refers to the casting between primitive data types. For example int to float.

Derivd casting refers to the casting between derived types/object.

What is auto widening and explicit narrowing in Java?

The data is implicitly casted from small sized primitive type to big sized primitive type. This is called auto-widening. i.e The data is automatically casted from byte to short, short to int, int to long, long to float and float to double..

You have to explicitly cast the data from big sized primitive type to small sized primitive type. i.e you have to explicitly convert the data from double to float, float to long, long to int, int to short and short to byte This is called explicit narrowing.

What is auto-up casting and explicit down casting in Java?

An object of sub class type can be automatically casted to super class type. This is called auto-up casting.

An object of super class type should be explicitly casted to sub class type, It is called explicit down casting.

«
»
volatile

Comments & Discussions