Define the type casting?, JAVA Programming

 

Type casting defines treating a variable of one type as though it is another type.

When up casting primitives as given below from left to right, automatic conversion happens. But if you derive from right to left, explicit casting or down casting is needed. Casting in Java is safer and easier than in C or other languages that allow arbitrary casting. Java only lets casts happen when they build sense, such as a cast between a float and an int. However you can't cast between a String and an int.

Byte, short, int, long, float, and double

int i = 5;

long j = i;         //Right. Implicit casting or up casting

byte b1 = i;      //Wrong casting. Compile time error "Type Mismatch".

byte b2 = (byte) i ;       //Right. Explicit casting or Down casting is required.

When it needs to object references you may always cast from a subclass to a superclass because a subclass object is also a superclass instance. You may cast an object implicitly to a super class type. If that were not the case polymorphism couldn't be possible.

 

 

 

2281_Untitled.png

 

You can cast down the hierarchy as well but you have to explicitly write the cast and the object must be a legitimate instance of the class you are casting to. The ClassCastException is given to indicate that code has tried to cast an object to a subclass of which it is not an instance of that class. If you are using J2SE 5.0 then "generics" will remove the need for casting otherwise you may deal with the problem of incorrect casting in two ways:

1.      Use the exception handling process to catch ClassCastException.

2.      Use the instance of statement to protect against incorrect casting.

 

Posted Date: 7/25/2012 2:53:54 AM | Location : United States







Related Discussions:- Define the type casting?, Assignment Help, Ask Question on Define the type casting?, Get Answer, Expert's Help, Define the type casting? Discussions

Write discussion on Define the type casting?
Your posts are moderated
Related Questions
The Naming class gives methods for storing and obtaining references to remote objects in the remote object registry.

What is an abstract class? Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie. you may not

These applications are like any Java application. They are made up of not many classes, each performing a particular purpose within the application. But these classes are configure

Problem : Compiler Design - Limit the loops Rajni is a newbie to the programming and while learning the programming language he came to know the following rules: ???• Each

I have to weave two positive integers together. For example, if the numbers are 137 and 064, the output integer would be 103647. This has to be done using integer arithmetic (not s

Describe advantage of the event-delegation model over the earlier event-inheritance model ?

Consider the following C++ template class. #include using namespace std; template class SortedList { public: SortedList() {size = 0;} void insert(T item); friend ostrea

JMX framework can enhance the manageability of your application by  1. Monitoring your application for critical events, performance problems, and error condition statistics. E.g

RMI is a set of APIs that permits to build distributed applications. RMI uses interfaces to describe remote objects to turn local method invocations into remote method invocations.

Explain Arithmetic Promotion and Binary Operations? An int divided through an int is an int, and a double divided by a double is a double, but what about an int divided by a do