aspose file tools*
The moose likes Beginning Java and the fly likes precedence: new, cast,  dot, Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "precedence: new, cast,  dot," Watch "precedence: new, cast,  dot," New topic
Author

precedence: new, cast, dot,

Anne Forumer
Ranch Hand

Joined: Apr 19, 2004
Posts: 72
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"!

What am I missing?

Thanks for clearing this up for me
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Well, that final "dot" can't be evaluated until there's an object for it, so that's why new goes first. Left to right, right?


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Andrew Eccleston
Ranch Hand

Joined: Jul 07, 2004
Posts: 140
I seem to recall doing something like this once:

char myChar = (new String("Hello")).charAt(0);


Though, I generally prefer to use two lines instead, for clarity's sake.


The statement below is true.<br />-------------------------------<br />The statement above is false.
Anne Forumer
Ranch Hand

Joined: Apr 19, 2004
Posts: 72
Why doesn't that imply that "new" has higher precedence than "dot"?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
When in doubt about operator precedence, you should use parentheses.

Layne


Java API Documentation
The Java Tutorial
Anne Forumer
Ranch Hand

Joined: Apr 19, 2004
Posts: 72
Originally posted by Layne Lund:
When in doubt about operator precedence, you should use parentheses.

Layne


True - But that doesn't really answer the question.
RD Tee
Greenhorn

Joined: Dec 23, 2004
Posts: 4
Object o = new String("Hello2");
// following won't compile
// myChar = (String)o.charAt(0);

Well the compiler error you would get there isn't a "new" vs. "dot" issue. You are getting the error because you are trying to cast a char into a String.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Wow, didn't anyone read Anne's post?

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: precedence: new, cast, dot,