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
Implement relationship using a java program. Assume each course has a course title, code, subject and number of credits while each student has a name, registration number, list of

Java platform is a software-only platform independent, which runs on top of other hardware-based platforms like WINDOWS, UNIX, NT etc.   The Java platform has 2 main com


Distributed / Network Oriented Java is network friendly -- both in its portable, threaded nature, and since common networking operations are built-in to the Java libraries.

What are the principle concepts of OOPS? Principle concepts upon that object oriented programming and design rest. They are: Abstraction: Abstraction refers to the act of

A easy expression language used by a JavaServer Faces UI component tag attributes to bind the associated component to a bean property or to bind the associated component's value to

What is the problem along with Relational Database and what solution you could suggest for it? When we store object orientated data within RDBMS it required to translate in to

Java Programming 1. Write a program in Java to find the highest of any five numbers. How do you compile and execute this Java program? 2. Write a program to explain the Exce

how to write aprogramm that calculates gross pay, taxes, and net pay for both hourly and commission employees. It will then display those results to the screen

Describe the final keyword ? The final keyword is used in various different contexts as a modifier meaning in which what it modifies cannot be changed in a few sense. final c