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
Polymorphism - defines the ability of a single variable of a provided type to be used to reference objects of different types, and automatically call the method that is specific

I want someone to fix some bugs in a java app Project Description: I have a small app in java, but it has some issues, the app is running. But we need to make it work more st

Need to develop a Small Android App using Text to speech and Speech to Text feature. Project Description: When the Android App Opens, It will connect to Windows Azure cloud,

J2EE (Java 2 Enterprise Edition) is an environment for deploying and developing enterprise applications on various operating system. The J2EE platform consists of J2EE components,

Implement your design in the Java programming language. Your main program file must be called MIPSim.java. Your simulator should implement the MIPS instruction set with the rest

What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer required by a pr


Object diagrams define instances instead of classes. They are useful for illustrating some complicated objects in detail about their recursive relationships.     When

Agile software development process is receiving popularity  and  momentum  across organizations. Highest priority is to satisfy the end user. Welcome requirement modifie

What is an immutable object? An immutable object is one that we can't change once it's created. Steps involved in creation of an immutable object are: Make all of its data f