Precedence tables tell me that "dot" operator has higher precedence than "new" and cast operators. The latter two have same precedence. According to this it makes sense that the following doesn't compile:
Object o = new String("Hello2"); // following won't compile // myChar = (String)o.charAt(0);
However, I can't explain that why does the following work:
char myChar = new String("Hello").charAt(0);
It seems that here "new" is getting higher precedence than "dot"!
I've tried two tests, one that creates a String and another that creates a different object, and both bind the new operator higher than the postfix . operator. I did the second test just in case the compiler was doing some String trickery as we all know it already special-cases String w.r.t. the + operator.
However, the precedence table I could find on Sun's site (using Google) is in the Java Nuts and Bolts tutorial. It clearly shows . having higher precedence than new, contrary to my testing using JDK 1.4.2_05.
All of the index entries in the JLS mentioning precedence only say "the Java implementation must respect the explicit rules of operator precedence" without stating them! I expect I'm just not reading the correct part of the JLS, or that I'm missing some nuance with how they are ordered in the spec, so please point it out if you know.
My take is that the new operator only makes sense at the highest precedence level. You cannot use "new <expression-returning-a-Class>", so what's the point of having . be higher than new? So I think what's happened is everyone's gone off of that Java tutorial to list new where it is, and they're all wrong.
[ After a little more Googling... ]
Here's a thread on another forum asking the same question: operator precedence. The answer posted is
The question was answered by John Bollinger in c.l.j.programmer. The answer is that the precedence table is wrong in this case.
Anyone have anything more "official" from Sun? [ February 10, 2005: Message edited by: David Harkness ]