Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is type-casting? and how can you achieve it?

user-image
Question added by himanshu mittal
Date Posted: 2013/07/03

Type casting in java is to cast one type class or interface to another type i.e.class or interface.superclass variable can't point subclass object in java i.e.can't call method which is declared in subclass.in order to do that you need to cast object back to its original type.
e.g.
suppose two class are First and Second.
Second extends First First f=new Second(); Second s=(Second) f; // typecasting First to Second

Mohamed Riadh Boumendjas
by Mohamed Riadh Boumendjas , developpeur JAVA EE , icon soft

is to cast one type to another type

Zabeer Sultan
by Zabeer Sultan , Senior Technical Lead , Happiestminds

Type casting is a mechanism of converting a reference of an object from one type to another compatible type.
Please note that the underlying object doesn't change.
e.g.
Assume Vehicle is a base class and we have sub classes say LMV and Truck.
Now refer following code Vehicle v = new Truck() // Create a new Truck object and hold it in a Vehicle reference Truck t = (Truck) v; // Even though v holds a Truck object, we cannot directly assign it to a Truck reference //as the reference is higher in hierarchy, so need to cast it.
There are two types of casting.
upcasting and downcasting, casting an object to any of its parent reference is called upcasting e.g.
Object o = t // no need to explictly mentoin the case as Object o = (Object) t as its implicit.
Casting an object reference to a child reference (provided the containing object is of the child type or any of its sub children) is called downcasting.
The earlier example Truck t = (Truck) v is actually downcasting

Typecasting is nothing but type conversion of the datatype of the source element to the data type of the destination element.Always remember: LHS=RHS.This is an assignment operation.here LHS is the destination and RHS is the source.Type casting is done because the operator(any operator unary or binary) DOES NOT KNOW HOW TO OPERATE WITH2 DIFFERENT DATATYPES.So,this leads to conversion.remember:every datatype will have its own set of unary and binary operators ofcourse same in case of bitwise,relational,logical operators.2 types of typecasting:implicit and explicit.in case of IMPLICIT compiler converts itself.

Advaitha Chidurala
by Advaitha Chidurala , Teacher , Little Masters Abacus

When you are are trying to hold a bigger data type value into a smaller dat type variable ,this process is called Explicit TypeCasting..If this Casting is done in Class-level then An error like Class CastException arises.

Aeshna Sachdeva
by Aeshna Sachdeva , Software Development-Intern , Ericsson

Type casting: Converting from one type to a different type.
E.g.
string text = new string("2"); int number = Integer.parseint(text); // Integer is a class and parseint is it's object.
-> The typecasting from string to integer using parseint is possible only if string contains a number.
If you try to do so with some text value instead of numeric value, you will end up screwing your compiler.
You cant do the typecasting for the type boolean.

Shruti Srivastava
by Shruti Srivastava , Guest researcher , PRI Wageningen University

Type-casting forces an explicit conversion of the type of a value. Type-casting is an operation which takes one operand. It operates on its operand, and returns an equivalent value in the specified type.

Javed Khan
by Javed Khan , Senior Programmer Analyst , Barclays Technology

Type casting is of two types Primitive data type casting which involves conversion of int, double, float, boolean etc.
The other type casting is reference type casting or more commonly known as object type casting.
There are brilliant links to understand these concepts on the net.

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.