Compilers – Java: my study notes

These notes are from the Week 10 of the Compilers course by Stanford.


One problem that Java arrays resolve lies in inheritance. If we have classes A and B which B inherits from A the relationship would still be valid for B[] < A[]. The problem would happen with the following code:

class A {


class B {
  void onlyOnB();


B[] b = ...
A[] a = b;
a[0] = new A();
b[0].onlyOnB(); // A crash would happen here!

The way Java solves this problem is by type checking during runtime and not allowing incompatible classes.


One way to implement exceptions is to mark the location whenever we encounter a try on the code. As soon as an exception is thrown we go back to that marker and execute the corresponding catch. The drawback here is that now using try-catch has an overhead.

Another characteristic of Java is exceptions that happen during the finalize method, which is called by the garbage collector, are simply thrown away and ignored.


Interfaces are usually slower than inheritance because the methods do not need to be at fixed offsets. One approach that solves this problem is to have a lookup table that translates method names to the actual methods.


Coercions are basically implemented by adding conversion functions when some piece of code needs to be transformed into another type. In java, there are two types of coercions widening and narrowing. The first one always succeeds but the second one might fail since it is transforming data to a more restrictive type. Also, widening can be implicit done by the compiler, but not narrowing.


The way threading can be implemented is to have a superclass representing it, and a general scheduler that chooses when to execute them and also manages the locks.


The ClassLoader has another type-checking phase when loading bytecode since it might not have come from a trusted compiler.

The Class Object is an object that has all the metadata of a class on it. It is used for reflection and other information that need to check the class metadata.

Leave a Reply

Your email address will not be published. Required fields are marked *