Raw types and subtyping

Posted by Dmitrii on Stack Overflow See other posts from Stack Overflow or by Dmitrii
Published on 2013-11-02T08:41:41Z Indexed on 2013/11/02 9:54 UTC
Read the original article Hit count: 263

Filed under:
|
|

We have generic class

SomeClass<T>{ }

We can write the line:

SomeClass s= new SomeClass<String>();

It's ok, because raw type is supertype for generic type. But

SomeClass<String> s= new SomeClass();

is correct to. Why is it correct? I thought that type erasure was before type checking, but it's wrong.

From Hacker's Guide to Javac

When the Java compiler is invoked with default compile policy it performs the following passes:

  1. parse: Reads a set of *.java source files and maps the resulting token sequence into AST-Nodes.
  2. enter: Enters symbols for the definitions into the symbol table.
  3. process annotations: If Requested, processes annotations found in the specified compilation units.
  4. attribute: Attributes the Syntax trees. This step includes name resolution, type checking and constant folding.
  5. flow: Performs data ow analysis on the trees from the previous step. This includes checks for assignments and reachability.
  6. desugar: Rewrites the AST and translates away some syntactic sugar.
  7. generate: Generates Source Files or Class Files.

Generic is syntax sugar, hence type erasure invoked at 6 pass, after type checking, which invoked at 4 pass. I'm confused.

© Stack Overflow or respective owner

Related posts about java

Related posts about compiler