answersLogoWhite

0

When to use casting in java?

Updated: 8/20/2019
User Avatar

Wiki User

11y ago

Best Answer

Casting must be done when converting an object from a "wider" or "parent" object to a "narrower" or "child" object. As an example, you do not need to cast from a byte to an int, but you must cast from an int to a byte; the compiler requires the cast so that the developer confirms that they are okay with a possible loss of data. Similarly, given the classes Vehicle, Car, and Truck, where Car and Truck are children of Vehicle, no cast is required to convert either a Car or Truck to a Vehicle, but the language requires a cast to convert a Vehicle to a Car or Truck.

In this case, if the Vehicle is cast to a Car, but is actually a Truck, the cast will fail and a runtime exception will be thrown. By casting a Vehicle to a Car, the developer acknowledges that they already know the Vehicle is a Car, and the compiler honors that arrangement. The Virtual Machine expects the Vehicle to be a Car, but if it is actually a Truck, the exception is thrown.

The objective of casting is to allow conversion between semi-compatible types of data where some loss of data may occur, but the developer has logically proven that the loss will never occur or is acceptable. You cannot cast a Car to an int, for example, because there is no possibility that a Car will ever be an int. The compiler will advise you of this fact and refuse to compile the code.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When to use casting in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why do use typecasting in java?

Type casting is used to convert from one datatype to the other


How do you change a float into a double using java?

with help of type casting it is possible


Why is there type casting in java?

Type casting exists to allow the use of base class or base interface reference but yet if you know that the object in hand is aspecific derived class, you can typecast and access methods available only in the derived class.


Can you use pointers in java?

Java does not support Pointers and hence you cannot use it in Java.


Is Float to float casting in java possible?

It is possible in JDK 1.5 and above. In JDK 1.4, you also cast the Float value into float value by using casting. For exampe in JDK 1.5, its possible float f = 12; Float ff = f; but in JDK 1.4, you will use casting i.e float f = 12; Float ff = (float)f;


What technologies link to Java?

Java is exclusive to surfing the web. Some web sites use Java to embed videos, others use Java for advertisements.


Does Java Script use the same compiler as Java?

No


What you use in case of pointer in java?

Java does not have pointers


What is java 7?

Java 7 is the most current version of Java. Many movies and games use Java.


Which is the best Book to understand JAVA in short?

You can use Head First Java or Java Complete Reference to learn Java.


Do you have to use java?

Not usually. There are numerous languages out there with similar functionality. The only time you must use Java is if your employer or client demands that a project be done in Java, or a particular platform requires the use of Java, such as Android OS.


How do you script a RuneScape bot?

RuneScape bots use Java. Use a Java Binary Code.