I'll illustrate an exxample of what i understand by precedence: int y = 2 >> 7 + 8 * 9; is evaluated as.... int y = 2 >> (7 + (8 * 9)); //(2 >> (7 + 72))=>(2 >> 79)
although the evaluation is always done from left to right but precedence decides how the grouping is done..........
Now i've a case: String s3 = new String("Hello").toString(); how is this evluated by the compiler as the order of precedence of "." (object member access operator) is higher than that of "new" operator, so it should be grouped as : new ( String("Hello").toString()); but that doesn't mean anyhing...... here first compiler is evaluating a new string object and on that its calling the toString() which doesn't actually follows the order of precedence ............
can someone clear my misconception which i'm here........
how is this evluated by the compiler as the order of precedence of "." (object member access operator) is higher than that of "new" operator, so it should be grouped as : new ( String("Hello").toString());
? I got this from the JLS,
15.12.4 Runtime Evaluation of Method Invocation
At run time, method invocation requires five steps. First, a target reference may be computed. Second, the argument expressions are evaluated. Third, the accessibility of the method to be invoked is checked. Fourth, the actual code for the method to be executed is located. Fifth, a new activation frame is created, synchronization is performed if necessary, and control is transferred to the method code.
If you read further in this section, it is clear that the target reference ( Eg.'this' ) is evaluated before anything else is done.
Joined: Mar 05, 2005
i totally concur !!! but my question is about precedence, actullay i got it cleared that we cant actually have a situation where we have "." be grouped b4 new, assuming they are contiguosly placed.
i mean, new (String("Hello").toString()) actually doesn't have any meaning as the String("Hello") is a constructor and not a method so we can't call it like this, as a constructor can only be called upon a "new" operator or from inside a constructor itself(i.e. either by super() or this())
1. Order of evaluation 2. Precedence of operators, and 3. Associativity.
1. Oder of evaluation:
All operands are evaluated first, from left two right. If one operand evaluation terminates abruptly (because an exception) then the whole expression terminates abrupltly because of the same expression and the rest of the operands are not evaluated.
In the expression a() + b() +c(), the three expressions will be evaluated first, before even applying any operator. Test this making a(), b() and c() write something to the standard output and you will see what I mean.
2. Precedence of operator:
When operators are diverse, this tells you wich operands should be affected first.
In the expression a() + b() * c() is equal to a() + ( b() * c() )
When operators are of the same precedence this tells you which operands should be affected first.
The expression a() + b() - c() is equal to ( a() + b() ) + c() because the + operator has left associativity.
Not the same that: a = b = c which would be evaluated a = (b = c), because the = has right associativity.
Hope this helps [ May 13, 2005: Message edited by: Edwin Dalorzo ]