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?

Writing a program with all the primitive, Problem 1 Explain and Define ...

Problem 1 Explain and Define the following concepts with appropriate examples a) Superclass b) Subclass c) Inheritance d) Exceptions e) Stream classes >>Defining and expl

Demonstrate your ability to ability to use java data , Purpose Demonstr...

Purpose Demonstrate your ability to ability to use Java data and control structures. Draw with the aid of conditionals and loops. Background Thought A note on comments:

I want software tool for sequential circuit analysis, I want Software Tool ...

I want Software Tool for Sequential Circuit Analysis The output of the program could be both graphical for the diagram and the rest is console. Skills required: C++ Progra

Develop a fantasy football, Need to develop a Fantasy football Project D...

Need to develop a Fantasy football Project Description: I am seeking to build a Fantasy football website. i already have an example of existing website. i want the exact same

Pseudocode, Create an application for an animal-fur trimming service. The b...

Create an application for an animal-fur trimming service. The business is open 15 weeks of the year, from April through July. The fee for a small animal (under 6 pounds) is $100, a

Write a code in java to explain the while loop in java, Write a code in jav...

Write a code in java to explain the while loop in Java? // This is the Hello program in Java class Hello { public static void main (String args[]) { System.out.print("

Define object-to-relational (o/r) mapping, Object-to-Relational (O/R) mappi...

Object-to-Relational (O/R) mapping : There are various ways to persist data and the persistence layer is one of the most important layers in any application development. O/R ma

Programming projects, Write a ComputePart class and a ComputerKit class(and...

Write a ComputePart class and a ComputerKit class(and a cliet class to test them) You should include the following methods: a method returning "expensive" if the total of the price

Code, pls telme source code for Padovan String

pls telme source code for Padovan String

Explain function system.arraycopy(), Explain function system.arraycopy() ? ...

Explain function system.arraycopy() ? Although copying an array isn't particularly hard, it is an operation that advantages from a native implementation. Thus java.lang.System

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