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.






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
In JavaScript, what is event handling? What are the two categories of events? In java script we could capture events on do a few operation on the basis of which particular even

The Web container is used for managing the servlet's life cycle. The Web container produces an instance of the servlet and then the container invokes the init() function. At the co

Explain BIOS in java language? BIOS basically stand for Basic input/output system it is also called as the system BIOS or ROM BIOS is a de facto standard describing a firmware

Describe synchronization in respect to multithreading. With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared res

Requested figure handle in use by another object, how do i fix this?

What is Mixing Data Types? As well as merging various operations, you can mix and match various numeric data types on the similar line. The program below uses both ints and dou

i) Set the number of data points to 20 and the number of clusters to 2. Check box Show History. Click on Initialize. Click on Start. Click on Step . Observe which points ch

What are adding methods? A subclass isn't restricted to changing the behavior of its superclass. It can also add fully latest techniques and fields that are not shared along wi

Question: (a) State and explain clearly FIVE new features of Actionscript 3.0 as compared to Actionscript 2.0. (b) Explain clearly the concept of events and events listen

How to Scaling Images in java applet? You can scale an image within a particular rectangle using this version of the drawImage() method: public boolean drawImage(Image img,