File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

precedence: new, cast, dot,

 
Anne Forumer
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1258
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Andrew Eccleston
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anne Forumer
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why doesn't that imply that "new" has higher precedence than "dot"?
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When in doubt about operator precedence, you should use parentheses.

Layne
 
Anne Forumer
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic