Jia Zheng Lua
Jia Zheng Lua

Reputation: 101

Compile time or Runtime for type casting/ type binding / type inference etc

I am relatively new to Java and I had an exam past year that got me stuck.

The task asked me to differentiate which options run at Compilation time / Runtime.

  1. Type casting.
  2. Late binding.
  3. Accessibility checking.
  4. Type inference.
  5. Type erasure.
  6. Type checking.

Are there any sources I can read to improve my understanding on static / dynamic binding?

Upvotes: 2

Views: 3290

Answers (1)

Xarvalus
Xarvalus

Reputation: 3021

  • Type casting - Compile time but could not be catched and throw ClassCastException (Java Type Casting)
  • Late binding - Runtime in general and Compile time for calls to final, private, or static methods (Late Binding in Java)
  • Accessibility checking - Access modifiers are solely a compile time mechanism in C++. In Java however, they are enforced at runtime too, because Java also has a runtime typesystem, and it can dynamically (at runtime) create classes. So it needs to enforce access at runtime too for types it doesn't know about at compile time. (OOP Access Modifiers: Compile-time or Run-time)
  • Type inference - Compile time - Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable (Type Inference)
  • Type erasure - Compile time - Type erasure can be explained as the process of enforcing type constraints only at compile time and discarding the element type information at runtime (Type Erasure in Java)
  • Type checking - Compile time but facilitating runtime type checking eg via instanceof (What are the things are checked at compile time by Java?)

Upvotes: 5

Related Questions