Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any type casting done by javac?

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?

like image 601
qinsoon Avatar asked Jul 24 '12 13:07

qinsoon


People also ask

Is there type casting in Java?

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.

Which type casting is automatically performed?

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.

Which of the type casting is valid in Java?

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.

What is type casting with example?

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.


2 Answers

Actually, there are three possibilities in this case:

  1. The javac compiler could perform the optimization.
  2. The JIT compiler could perform the optimization.
  3. The native code by the JIT compiler could include code to do a runtime type check.

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

}
like image 166
Stephen C Avatar answered Oct 04 '22 12:10

Stephen C


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.

like image 28
developer Avatar answered Oct 04 '22 11:10

developer