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?

Develop a custom android app, Develop a Custom Android App Project Descr...

Develop a Custom Android App Project Description: We want you to construct a custom Android app. Once user downloads the app and upon installation the user gets push notif

Answer, a program to find the area under curve y=f(x) between x=a and x=b,i...

a program to find the area under curve y=f(x) between x=a and x=b,integrate y=f(x) between the limits of a and b

Develop a supply chain management, Develop a Supply Chain Management Pro...

Develop a Supply Chain Management Project Description: Supply Chain Management from HK and China Skills required is Java

What is testing for equality in java, What is Testing for Equality in java?...

What is Testing for Equality in java? Testing for equality is a little trickier. You would expect to test if two numbers are equal through using the = sign. However the = sign

Want a anti iframe breaker, Want a ANTI Iframe Breaker Project Descripti...

Want a ANTI Iframe Breaker Project Description: I want an ANTI Iframe Breaker code made, so that if I'm iframing a site, that contains JavaScript, flash or otherwise, to forc

I need 3d globe application, I need 3D Globe (Android) application Proje...

I need 3D Globe (Android) application Project Description: Seeking a talented android programmer to prepare a simple app for Android tablet display which can feature a 3D glo

Determine the phase loading- java program development, Determine the phase ...

Determine the phase Loading- Java Program Development In phase 3, the program must first be placed in memory before it can be executed. This is done by the class loader, whic

Debugging, Debugging mode can be enabled by adding the following line of co...

Debugging mode can be enabled by adding the following line of code before creating new connections: XMPPConnection.DEBUG_ENABLED = true; When debugging mode is enabled, 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