title: How does a Java compiler parse typecasts?
body: <p>A simple expression like</p>
  
  <pre><code>(x) - y
  </code></pre>
  
  <p>is interpreted differently depending on whether <code>x</code> is a type name or not. If <code>x</code> is not a type name, <code>(x) - y</code> just subtracts <code>y</code> from <code>x</code>. But if <code>x</code> is a type name, <code>(x) - y</code> computes the negative of <code>y</code> and casts the resulting value to type <code>x</code>.</p>
  
  <p>In a typical C or C++ compiler, the question of whether <code>x</code> is a type or not is answerable because the parser communicates such information to the lexer as soon as it processes a typedef or struct declaration. (I think that such required violation of levels was the nastiest part of the design of C.)</p>
  
  <p>But in Java, <code>x</code> may not be defined until later in the source code. How does a Java compiler disambiguate such an expression? </p>
  
  <p>It's clear that a Java compiler needs multiple passes, since Java doesn't require declaration-before-use. But that seems to imply that the first pass has to do a very sloppy job on parsing expressions, and then in a later pass do another, more accurate, parse of expressions. That seems wasteful.</p>
  
  <p>Is there a better way? </p>