answersLogoWhite

0

Define type casting in java

Updated: 8/10/2023
User Avatar

Wiki User

14y ago

Best Answer

Type casting means to explicitly convert one type to another.

For example, the following lines:

double x = 5.0;
int y = x;

will produce an error message, because of a possible loss of precision (any decimals will get lost when converting to int). The following will work however:

double x = 5.0;
int y = (int) x;

The programmer is forcing the Java compiler to accept the conversion; saying, in effect: "Please do this anyway, I know what I am doing".

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

In java object typecasting one objectreference can be type cast into another object reference. The cast can be to its own class type or to one of its subclass or superclass types or interfaces. There are compile-time rules and runtime rules for casting in java.

How to Typecast Objects with a dynamically loaded Class ? - The casting of object references depends on the relationship of the classes involved in the same hierarchy. Any object reference can be assigned to a reference variable of the type Object, because the Object class is a superclass of every Java class.

There can be 2 casting java scenarios · Upcasting

· Downcasting

When we cast a reference along the class hierarchy in a direction from the root class towards the children or subclasses, it is a downcast. When we cast a reference along the class hierarchy in a direction from the sub classes towards the root, it is an upcast. We need not use a cast operator in this case. The compile-time rules are there to catch attempted casts in cases that are simply not possible. This happens when we try to attempt casts on objects that are totally unrelated (that is not subclass super class relationship or a class-interface relationship) At runtime a ClassCastException is thrown if the object being cast is not compatible with the new type it is being cast to.

Below is an example showing when a ClassCastException can occur during object casting //X is a supper class of Y and Z which are sibblings. public class RunTimeCastDemo { public static void main(String args[]) { X x = new X(); Y y = new Y(); Z z = new Z(); X xy = new Y(); // compiles ok (up the hierarchy) X xz = new Z(); // compiles ok (up the hierarchy) // Y yz = new Z(); incompatible type (siblings) // Y y1 = new X(); X is not a Y // Z z1 = new X(); X is not a Z X x1 = y; // compiles ok (y is subclass of X) X x2 = z; // compiles ok (z is subclass of X) Y y1 = (Y) x; // compiles ok but produces runtime error Z z1 = (Z) x; // compiles ok but produces runtime error Y y2 = (Y) x1; // compiles and runs ok (x1 is type Y) Z z2 = (Z) x2; // compiles and runs ok (x2 is type Z) // Y y3 = (Y) z; inconvertible types (siblings) // Z z3 = (Z) y; inconvertible types (siblings) Object o = z; Object o1 = (Y) o; // compiles ok but produces runtime error } }

In general an implicit cast is done when an Object reference is assigned (cast) to: * A reference variable whose type is the same as the class from which the object was instantiated.

An Object as Object is a super class of every Class.

* A reference variable whose type is a super class of the class from which the object was instantiated.

* A reference variable whose type is an interface that is implemented by the class from which the object was instantiated.

* A reference variable whose type is an interface that is implemented by a super class of the class from which the object was instantiated. Consider an interface Vehicle, a super class Car and its subclass Ford. The following example shows the automatic conversion of object references handled by the compiler interface Vehicle {

}

class Car implements Vehicle {

} class Ford extends Car {

} Let c be a variable of type Car class and f be of class Ford and v be an vehicle interface reference. We can assign the Ford reference to the Car variable:

I.e. we can do the following '''Example 1

c = f; //Ok Compiles fine''' Where c = new Car();

And, f = new Ford();

The compiler automatically handles the conversion (assignment) since the types are compatible (sub class - super class relationship), i.e., the type Car can hold the type Ford since a Ford is a Car. '''Example 2

v = c; //Ok Compiles fine

c = v; // illegal conversion from interface type to class type results in compilation error''' Where c = new Car();

And v is a Vehicle interface reference (Vehicle v) The compiler automatically handles the conversion (assignment) since the types are compatible (class - interface relationship), i.e., the type Car can be cast to Vehicle interface type since Car implements Vehicle Interface. (Car is a Vehicle). Casting Object References: Explicit Casting Sometimes we do an explicit cast in java when implicit casts don't work or are not helpful for a particular scenario. The explicit cast is nothing but the name of the new "type" inside a pair of matched parentheses. As before, we consider the same Car and Ford Class class Car {

void carMethod(){

}

} class Ford extends Car {

void fordMethod () {

}

} We also have a breakingSystem() function which takes Car reference (Superclass reference) as an input parameter.

The method will invoke carMethod() regardless of the type of object (Car or Ford Reference) and if it is a Ford object, it will also invoke fordMethod(). We use the instanceof operator to determine the type of object at run time. public void breakingSystem (Car obj) {

obj.carMethod();

if (obj instanceof Ford) ((Ford)obj).fordMethod ();

} To invoke the fordMethod(), the operation (Ford)obj tells the compiler to treat the Car object referenced by obj as if it is a Ford object. Without the cast, the compiler will give an error message indicating that fordMethod() cannot be found in the Car definition.

The following program shown illustrates the use of the cast operator with references. Note: Classes Honda and Ford are Siblings in the class Hierarchy. Both these classes are subclasses of Class Car. Both Car and HeavyVehicle Class extend Object Class. Any class that does not explicitly extend some other class will automatically extends the Object by default. This code instantiates an object of the class Ford and assigns the object's reference to a reference variable of type Car. This assignment is allowed as Car is a superclass of Ford. In order to use a reference of a class type to invoke a method, the method must be defined at or above that class in the class hierarchy. Hence an object of Class Car cannot invoke a method present in Class Ford, since the method fordMethod is not present in Class Car or any of its superclasses. Hence this problem can be colved by a simple downcast by casting the Car object reference to the Ford Class Object reference as done in the program. Also an attempt to cast an object reference to its Sibling Object reference produces a ClassCastException at runtime, although compilation happens without any error. class Car extends Object { void carMethod() { } } class HeavyVehicle extends Object { } class Ford extends Car { void fordMethod() { System.out.println("I am fordMethod defined in Class Ford"); } } class Honda extends Car { void fordMethod() { System.out.println("I am fordMethod defined in Class Ford"); } } public class ObjectCastingEx { public static void main(String[] args) { Car obj = new Ford(); // Following will result in compilation error // obj.fordMethod(); //As the method fordMethod is undefined for the Car Type // Following will result in compilation error // ((HeavyVehicle)obj).fordMethod(); //fordMethod is undefined in the HeavyVehicle Type // Following will result in compilation error ((Ford) obj).fordMethod(); //Following will compile and run // Honda hondaObj = (Ford)obj; Cannot convert as they are sibblings } } One common casting that is performed when dealing with collections is, you can cast an object reference into a String. import java.util.Vector; public class StringCastDemo { public static void main(String args[]) { String username = "asdf"; String password = "qwer"; Vector v = new Vector(); v.add(username); v.add(password); // String u = v.elementAt(0); Cannot convert from object to String Object u = v.elementAt(0); //Cast not done System.out.println("Username : " + u); String uname = (String) v.elementAt(0); // cast allowed String pass = (String) v.elementAt(1); // cast allowed System.out.println(); System.out.println("Username : " + uname); System.out.println("Password : " + pass); } } Output Username : asdf

Username : asdf

Password : qwer = instanceof Operator = The instanceof operator is called the type comparison operator, lets you determine if an object belongs to a specific class, or implements a specific interface. It returns true if an object is an instance of the class or if the object implements the interface, otherwise it returns false. Below is an example showing the use of instanceof operator class Vehicle { String name; Vehicle() { name = "Vehicle"; } } class HeavyVehicle extends Vehicle { HeavyVehicle() { name = "HeavyVehicle"; } } class Truck extends HeavyVehicle { Truck() { name = "Truck"; } } class LightVehicle extends Vehicle { LightVehicle() { name = "LightVehicle"; } } public class InstanceOfExample { static boolean result; static HeavyVehicle hV = new HeavyVehicle(); static Truck T = new Truck(); static HeavyVehicle hv2 = null; public static void main(String[] args) { result = hV instanceof HeavyVehicle; System.out.print("hV is an HeavyVehicle: " + result + "\n"); result = T instanceof HeavyVehicle; System.out.print("T is an HeavyVehicle: " + result + "\n"); result = hV instanceof Truck; System.out.print("hV is a Truck: " + result + "\n"); result = hv2 instanceof HeavyVehicle; System.out.print("hv2 is an HeavyVehicle: " + result + "\n"); hV = T; //Sucessful Cast form child to parent T = (Truck) hV; //Sucessful Explicit Cast form parent to child } } Output hV is an HeavyVehicle: true

T is an HeavyVehicle: true

hV is a Truck: false

hv2 is an HeavyVehicle: false Note: hv2 does not yet reference an HeavyVehicle object, instanceof returns false. Also we can't use instanceof operator with siblings

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Type casting means to explicitly convert one type to another.

For example, the following lines:

double x = 5.0;
int y = x;

will produce an error message, because of a possible loss of precision (any decimals will get lost when converting to int). The following will work however:

double x = 5.0;
int y = (int) x;

The programmer is forcing the Java compiler to accept the conversion; saying, in effect: "Please do this anyway, I know what I am doing".

Type casting means to explicitly convert one type to another.

For example, the following lines:

double x = 5.0;
int y = x;

will produce an error message, because of a possible loss of precision (any decimals will get lost when converting to int). The following will work however:

double x = 5.0;
int y = (int) x;

The programmer is forcing the Java compiler to accept the conversion; saying, in effect: "Please do this anyway, I know what I am doing".

Type casting means to explicitly convert one type to another.

For example, the following lines:

double x = 5.0;
int y = x;

will produce an error message, because of a possible loss of precision (any decimals will get lost when converting to int). The following will work however:

double x = 5.0;
int y = (int) x;

The programmer is forcing the Java compiler to accept the conversion; saying, in effect: "Please do this anyway, I know what I am doing".

Type casting means to explicitly convert one type to another.

For example, the following lines:

double x = 5.0;
int y = x;

will produce an error message, because of a possible loss of precision (any decimals will get lost when converting to int). The following will work however:

double x = 5.0;
int y = (int) x;

The programmer is forcing the Java compiler to accept the conversion; saying, in effect: "Please do this anyway, I know what I am doing".

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Type Conversion Type Conversion is that which converts the one data type into another for example converting a int into float converting a float into double The Type Conversion is that which automatically converts the one data type into another but remember we can store a large data type into the other for ex we can t store a float into int because a float is greater than int Type Conversion is Also Called as Promotion of data Because we are Converting one Lower data type into higher data type So this is Performed Automatically by java compiler..

Type Casting

When a user can convert the one higher data type into lower data type then it is called as the type casting Remember the type Conversion is performed by the compiler but a casting is done by the user for ex converting a float into int Always Remember that when we use the Type Conversion then it is called the promotion when we use the type casting means when we convert a large data tote into another then it is called as the demotion when we use the type casting then we can loss some data. Like We are converting float into int then it will Truncate the Fractional part from the data Like (int) 123.78 Will gives use only 123 this will Truncate the Fractional Part and Returns only Int value

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

It means that you explicitly convert from one data type to another. For example, Java won't accept the following statements, because data may be lost, because a long variable can hold larger numbers:

long longVar = 5;

int intVar;

intVar = longVar;

However, if you type the last statement as:

intVar = (int) longVar;

you are basically telling the compiler that you know what you are doing, and that it should do the conversion anyway. (Of course, data may still be lost, so the programmer should be careful.)

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

It's the explicit conversion from one data type to another.

Example: assume number is a floating point number with a random value (double number = 70.4). In order to convert this data type to an integer, for example, casting is needed and it is usually done as such: int number_2 = (int) number. In our case, the value assigned to number_2 after casting would be the integral part of number = 70.4 (i.e 70)

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

The Term type casting is used in Java when an object of one type is converted to other. This casting can happen only between compatible object types.

for example you can cast a String into an Object but you cannot cast a String into an integer

Example:

String name = "Tommy";

Object obj = (Object) name;

System.out.println(obj.toString());

This would print "Tommy" on the console. Here we have tried to cast an object of type String into an Object. Since the Object data type is the parent data type for String the JVM allows this type casting.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

program for type casting in java

This answer is:
User Avatar

Add your answer:

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

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

with help of type casting it is possible


Why do use typecasting in java?

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


Can you define variables in interface in java?

yes we can define a variable in an interface in java.


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.


Define set in java?

A Java set is a collection of things that do not contain duplicate elements. Duplicates are prohibited from Java sets.


What is the hierarchy of the java program?

define the data types


What is function in java terminology?

In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.


How do you uss static in java?

It is uss to define class and method of pogrom's.


Name two high level languages and define them?

Java C++


Is java virtual machine dangerous?

JVM is a critical component in almost all Java platforms. I would hardly define it as "dangerous".


What type of government in java?

Java has a Communist government.


How do you define one-to-n mapping in java?

well first please go and play Minecraft on java and then a crepper will tell you👍👍