To my understanding, if type checking can be done during compilation, the type casting will be done during compilation and will not incur any runtime overhead.
For example
public Child getChild() {
Parent o = new Child();
return (Child) o;
}
Is the type casting done during compilation or during runtime?
And is there any general rule to decide if a type casting is done by javac compiler or by the VM?
The process of converting the value of one data type ( int , float , double , etc.) to another data type is known as typecasting. In Java, there are 13 types of type conversion.
Widening Type Casting It is done automatically. It is safe because there is no chance to lose data. It takes place when: Both data types must be compatible with each other.
There are two types of casting in Java as follows: Widening Casting (automatically) – This involves the conversion of a smaller data type to the larger type size. Narrowing Casting (manually) – This involves converting a larger data type to a smaller size type.
Typecasting, or type conversion, is a method of changing an entity from one data type to another. It is used in computer programming to ensure variables are correctly processed by a function. An example of typecasting is converting an integer to a string.
Actually, there are three possibilities in this case:
javac
compiler could perform the optimization.I expect that it is option 1. or 2. but this could be platform specific.
In fact, on my system the bytecode is not optimized. If any optimization is to occur it will be up the the JIT compiler to do it. (This fits with what I've heard ... that most Java bytecode compilers do little in the way of optimization before generating bytecodes.)
Compiled from "Test.java"
public class Test extends java.lang.Object{
public Test();
Code:
0: aload_0
1: invokespecial #8; //Method java/lang/Object."<init>":()V
4: return
public Child getChild();
Code:
0: new #16; //class Child
3: dup
4: invokespecial #18; //Method Child."<init>":()V
7: astore_1
8: aload_1
9: checkcast #16; //class Child
12: areturn
}
When a running program attempts to cast an object reference to another type, the virtual machine must check to see if the type being cast to is the actual class of the referenced object or one of its supertypes. It must perform the same kind of check when a program performs an instanceof operation.
In either case, the virtual machine must look into the class data of the referenced object. When a program invokes an instance method, the virtual machine must perform dynamic binding: it must choose the method to invoke based not on the type of the reference but on the class of the object. To do this, it must once again have access to the class data given only a reference to the object.
Edit:
Java compiler is not responsible for checking if the casting is correct or not, just like some of the bindings only occur at run time. Java virtual machine does the checking at run time to find out whether the actual reference object is a legitimate object of the new type. If not, there will be a runtime exception: ClassCastException.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With