aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator precedence question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operator precedence question" Watch "Operator precedence question" New topic
Author

Operator precedence question

Peter Du
Greenhorn

Joined: Oct 29, 2004
Posts: 14
The following code when compile is OK, but from the point of operator precedence view, it seems it should not be OK, because operator new has lower precedence than operator . :
new MyClass().myMethod();
Thanks in advance.


Peter Du<br /> <br />SCJP1.4 SCWCD1.4 SCBCD1.3
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
I am surprised also. According to the Sun's own online tutorial (http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html) the dot operator has a higher precedence than the new operator.

Yet the following compiles, and lines //1 and //2 seem logically equivalent. The implication is that operators new and dot have the same precedence and are evaluated from left to right.

Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
new Foo().myMethod(); //1

Hi ... I am not sure on what I write below! I want some experts to clarify on this. Anyways, thought of providing another pointer to look at this question.

See ... Syntax wise the compiler PASSes this statement as the instance method "myMethod()" is invoked on an object instance.

Now when executing this statement, HOW DO YOU PEOPLE SAY THAT 'new' IS EXECUTED FIRST !!! See the way I interpret!

First "." operator gets executed as it has higher precedence than 'new'. Now JVM looks and comes to know that myMethod() is called, but it is confused on which instance it is called. So it then executes, the 'new' statement and then calls the method on that instance.

If the interpretation is like what I have written just above, then the code behaves as per JLS, without bypassing the precedence rules. Right?

Again, I am not sure on this. Is there any way to confirm on this???


Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I think the compiler treats "new myClass().myMethod;" as "new( myClass() ).myMethod();".

Compilers encounter tokens from left to right and, on encountering the new operator, expect to see an identifier followed by a left paren, possibly a parameter list, and a right paren (I omit several other cases here). This does not follow the precedence rules, but see the following quote from the Java Language Specification:
15.7.5 Evaluation Order for Other Expressions
The order of evaluation for some expressions is not completely covered by these general rules, because these expressions may raise exceptional conditions at times that must be specified. See, specifically, the detailed explanations of evaluation order for the following kinds of expressions:

class instance creation expressions (�15.9.4)
array creation expressions (�15.10.1)
method invocation expressions (�15.12.4)
array access expressions (�15.13.1)
assignments involving array components (�15.26)


The final authority on Java syntax as implemented by Java compilers is not the precedence table but the context-free grammars in the Java Language Specification which are, in turn, the input to the JavaCC parser generator and similar programs. You could go through chapter 15 and verify the parsing of "new MyClass().myMethod();".


Mike Gershman
SCJP 1.4, SCWCD in process
Peter Du
Greenhorn

Joined: Oct 29, 2004
Posts: 14
Thanks for your reply.
I understand better now. new MyClass() is translated to a call to the constructor at compile time. At runtime the constructor run first to create an instance of the MyClass, then the instance method MyMethod() is called.

Thanks again.

Peter Du
 
Don't get me started about those stupid light bulbs.
 
subject: Operator precedence question