Define the type casting?, JAVA Programming

Assignment Help:

 

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.

 


Related Discussions:- Define the type casting?

Game of life, Conway's Game of Life is a "cellular automaton" that is playe...

Conway's Game of Life is a "cellular automaton" that is played on a 2D grid (array) of cells. At the start of the game, an initial configuration is set up in which a number of cell

Exportobject of unicastremoteobject do, What does the exportObject of Unica...

What does the exportObject of UnicastRemoteObject do? Ans) Exports the remote object to make it available to receive incoming calls, using the certain supplied port. If port not

In javascript, In JavaScript, what is event handling? What are the two type...

In JavaScript, what is event handling? What are the two types of events? In JavaScript capturing events and responding to them is event handling. The system sends events to the

User defined key class in the hashtables aur hashmap, You should override ...

You should override the hashCode() and equals() functions from the Object class. The default implementation of the hashcode() and equals(), which are inherited from the java.

Need remote synchronization tool for folders and files, Need Remote Synchro...

Need Remote Synchronization tool for folders and files? Project Description:                 We want a tool to synchronize the content of one or more folders on the file syst

USES OF JAVA PROGRAMMING, WHY WE USE JAVA IN PROGRAMMING INSTEAD OF OTHER P...

WHY WE USE JAVA IN PROGRAMMING INSTEAD OF OTHER PROGRAMMING LANGUAGE

Publish subscribe model and p2p model, What is the basic difference among P...

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

Multiple choices output and codes, codes and output on how to make multiple...

codes and output on how to make multiple choices quiz

Java program to scrape, Java Program to Scrape, Data Mine Project Descri...

Java Program to Scrape, Data Mine Project Description: I need to data scrape/ mine data from a website and do some data processing. Skills required: Java, Data Mining,

Advanced java info1414, This is a working program that shows election resul...

This is a working program that shows election results from the infamous 2000 presidential election involving a lot of Floridians and 'hanging chads'. The program as written reads a

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd