programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# how to solve expression ...

Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:
what will be the output of the following line :

System.out.println(5*6/2+9/3*2/7%2-10);

The answer is 5 . But I am confuse how to solve this .

thanks a lot .

Ranch Hand
Posts: 411
• Number of slices to send:
Optional 'thank-you' note:
Rathi,
Refer to operator precedence chart here

Hope that helps you.

Ranch Hand
Posts: 961
• Number of slices to send:
Optional 'thank-you' note:
The expression is evaluated from left to right, respecting the precedence of the operators.

It will be this way:
5 * 6 / 2 + 9 / 3 * 2 % 2 - 10

(5 * 6 / 2) + (9 / 3 * 2 % 2) - 10

As the operators * / % have the same precedence the expressions in the parenthesis are evaluated from left to right

The first parenthesis:
5 * 6 = 30
(5 * 6) / 2 = 15

The second expresion would be:
9 / 3 = 3
(9 / 3) * 2 = 6
( (9 / 3) * 2) / 7 = 0

Remember, all integer literals are converted to integers. So that last one is an integer division. Then...

( ( ( 9 / 3) * 2) / 7 ) % 2 = 0

So the final expression would be

15 + 0 - 10

Result is 5.

ankur rathi
Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:
Thanks Jay ,
I got the answer of this problem .
But that table doesn't include | , & , ^ operator .
Can you tell me where they fit in that table & what is the sequence like right to left or left to right .
Thanks once again .

ankur rathi
Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:
int i = 9 ;

System.out.println(i+++i+++i);

thanks edwin , please can you solve one more ...

thanks once again .
[ January 14, 2005: Message edited by: rathi ji ]

Edwin Dalorzo
Ranch Hand
Posts: 961
• Number of slices to send:
Optional 'thank-you' note:
Ok, let me see...

The expression would be evaluated this way:

i + (++i) + (++i)

So as i=9 the evaluation will be as follows

9 + (10) + (11)

The result is 30.

Jay Pawar
Ranch Hand
Posts: 411
• Number of slices to send:
Optional 'thank-you' note:

Original post:

But that table doesn't include | , & , ^ operator .
Can you tell me where they fit in that table & what is the sequence like right to left or left to right ?

click me
[ January 14, 2005: Message edited by: Jay Pawar ]

ankur rathi
Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:
Thanks Jay ...
You remembered whole table for exam ...

ankur rathi
Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:
Hi Jay & Edwin ,
According to this table this expression will be solved like this :

false && true | true

false && ( true | true )

false && true

false

Am I right ?

ankur rathi
Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:

According to this table | has higher priority than && . So the statement should be solved like this ( No matter , && is short circuit operator ) :

false && ( true | m() )

false && true

false

and the output should be : I called

but why not so ...

thanks a lot .

Jay Pawar
Ranch Hand
Posts: 411
• Number of slices to send:
Optional 'thank-you' note:
Rathi,
I did not memorize the entire table. I just remembered the important ones. I made search in this forum and came up with this old thread.
Check this
[ January 14, 2005: Message edited by: Jay Pawar ]

ankur rathi
Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:
Thanks a lot Jay , What about my previous question . It was really good one .
[ January 15, 2005: Message edited by: rathi ji ]

Ranch Hand
Posts: 1272
• Number of slices to send:
Optional 'thank-you' note:
System.out.println(false && true | true); prints "false"

ankur rathi
Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:
No Mike , this one ...

According to this table | has higher priority than && . So the statement should be solved like this ( No matter , && is short circuit operator ) :

false && ( true | m() )

false && true

false

and the output should be : I called

but why not so ...

thanks a lot .

Jay Pawar
Ranch Hand
Posts: 411
• Number of slices to send:
Optional 'thank-you' note:
Considering the following operator precedence given in Thinking in Java by Bruce Eckel.

OPERATOR PRECEDENCE

Ulcer Unary operators (),[],new, ++,--(postfix)
++,--,+,-,~,! (prefix)
+,-
<<,>>,>>>
Really Relational <,>,<=,>=,instanceof
==,=!
Like Logical &&,|| ,&,|,^
C Conditional A>B?x:y
A Lot Assignment *=,+=,etc.

It seems like in an expression having mixture of short circuit operators ( && and || ) and non short circuit operators ( & and | ), one must just have follow the left to right associativity.

Surprisingly, JLS doesn't show any operator precedence table.

Mike Gershman
Ranch Hand
Posts: 1272
• Number of slices to send:
Optional 'thank-you' note:
| is evaluated first. The left operand is the result of false&&true. The right operand is m(). Under the rules for |, both operands are always evaluated.

ankur rathi
Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:
No but , why the above class is not printing : I called
thanks a lot .

Mike Gershman
Ranch Hand
Posts: 1272
• Number of slices to send:
Optional 'thank-you' note:

false && true | m()
the above class is not printing : I called

Ok, I was wrong. I should always test a result before saying anything.

The abstract syntax tree is:

The idea is that precedence controls grouping of operations, not order of operations. The operand true binds to | rather than && because | has higher precedence. The other two rules are (1) associativity, usually left to right, for same-precedence operators, and (2) both operands are evaluated, left then right, before the operator.

Now consider this expression:
true | m() && false

The abstract syntax tree is:

Now m() gets called.
[ January 15, 2005: Message edited by: Mike Gershman ]

ankur rathi
Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:
It means that precedence is used only in grouping . execution always begin from left to right .

thanks a lot .

Mike Gershman
Ranch Hand
Posts: 1272
• Number of slices to send:
Optional 'thank-you' note:
In evaluating an expression, you have to consider all three rules - precedence, associativity, and operands before operator evaluation. Even parentheses force grouping, not order of evaluation. I think that's why James Gosling won't publish a Java precedence table despite many requests - it can be misleading.

It might help you to create the abstract syntax tree for some expressions. google "Abstract Syntax Tree" and you'll find plenty of material.

Edwin Dalorzo
Ranch Hand
Posts: 961
• Number of slices to send:
Optional 'thank-you' note:
The answer is in the Java Language Specification (15.23)

The && operator is like & (�15.22.2), but evaluates its right-hand operand only if
the value of its left-hand operand is true.

Please note that the decision regarding evaluating the rest of the operands is taken at evaluation time, and not at the time the operator is applied.

So, in the example above...

The left-most expressions will be evaluated first and as it is false, the rest of the expressions will never be evaluated.

Eximne this code snippet I wrote and you will realize operators are been applied in the correct order:

Always remember that the operands are first evaluated before operators are applied.

ankur rathi
Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:

Hi Edwin ,
I changed your code sightly . you are saying that operands are evaluated first before appling operator . No , I think you are wrong . This code even doesn't evaluated other operands because JVM got false at first place .

I think my statements was right in previous post .
1] grouping according to preference .
2] evaluate left to right .

Edwin Dalorzo
Ranch Hand
Posts: 961
• Number of slices to send:
Optional 'thank-you' note:
Yes, I know. That's exactly what I quote from the JLS

The && operator is like & (�15.22.2), but evaluates its right-hand operand only if
the value of its left-hand operand is true.

However, in my example all operands are evaluated. The Output is

sayTrue1
sayFalse
sayTrue2
true

Which means the three operands are evaluated before the application of the operators. But operators are aplied in their precedence order.

I the case of the code snippet that originated this discussion, just the first operand is evaluated. The rest, as you say, are not evaluated because the first operand is false. That's exactly what I quote above.

 Fire me boy! Cool, soothing, shameless self promotion: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com