Skip to content

Compile time vs Run time Summary

bingmeow1999 edited this page Nov 30, 2020 · 5 revisions
Compile-time Run-time
Type Left hand side of the assignment,
fixed upon declaration
Right hand side of the assignment,
vary as program runs
Binding Static Dynamic
Control Restricts methods that can be called based on compile-time type Methods to invoke are determined during run-time
Check for Method overloading,
more specific method will be invoked
Method overriding,
overridden method will never be invoked

What happens during Compilation Time in Java?

  1. Type Inference
  • Inferring the type of a variable whose types are not defined.
  1. Type Erasure
  • Replacing a type parameter of generics with either Object or its Bound.

What happens during Run Time in Java?

  1. Type Checking
  • Checking if the value matches the type of the variable it is assigned to. It happens during run-time since the value is not always available at compile time.
  1. Late Binding
  • Determines which instance method to call depending on the type of a reference object.

Exceptions

  1. Type Casting
  • Converting the type of one variable to another.
  • Happens during Compile time but checked during run time.
    • There are 2 types of casting: Widening Casting & Narrowing Casting
  • Widening Casting: assigning a smaller data type to a larger relevant data type. eg: int -> float
        jshell> int i = 10;
        i ==> 10
        jshell> float change = i;
        change ==> 10.0

The above illustrates the implicit casting which differs from narrowing casting. There is no need to wrap () to change the type for objects.

  • Narrowing Casting: [we saw this alot in class] Converts a bigger datatype to a smaller type. eg: equals method we witnessed in class
        @Override
        public boolean equals(Object obj){
                if(this == obj){
                        return true;
                }else if(obj instanceof Instructor){
                        Instructor n = (Instructor) obj;
                        return n.name.equals(this.name);
                }else{
                        return false;
                }
        }

If you noticed, there is a line which says Instructor 'n = (Instructor) obj;'. When the equals method is taking in an Object, an Object is like the mother class. Instructor here is a smaller size type than Object so, in order to change the type of Instructor, manual casting is needed to cast the obj taken in into an Instructor class object.

  1. Accessibility Checking
  • Checks if a class has access to a field in another class.
  • Happens in both Compile and Run Time.
Clone this wiki locally