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
Task I have an Android mobile app project that I''m trying to get profiles to transfer by connecting to a server at 23.23.175.213 port 1337. I have to send a json string to the se

What is the basic difference among Publish Subscribe model and P2P model? Ans) Publish Subscribe model is typically used in one-to-many situation. It is unreliable but very quic

Ask questionWrite a program BruteForceSearch that uses the brute-force approach given above and compare its running time on your computer with that of Binary Search for largeW.txt

How can we involve images within a web page using HTML and Java script? Images can be included in HTML through employing tag, below is the format of

I want Linux server expert with Tomcat skills Project Description: I want a Linux server expert to look in to an issue with my domain. It is down since three days as there is

1- I need application used android studio with source code and all file . same this application : 2-">https://play.google.com/store/apps/details?id=com.magnetic.openmaps&hl=en 2


Create an object model for the Solar System using the following link: Provide">http://airandspace.si.edu/etp/ss/index.htm Provide an abstract class called Planet and a concrete s

any example of games that is created by java ?

What is Color in AWT? Color is a class in the AWT. Individual colors such as red or mauve are examples of this class, java.awt.Color. Be sure to import it if you want to use ot