Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Java Interview Guide this week in the Jobs Discussion forum!

# how to solve expression ...

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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 .
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Rathi,
Refer to operator precedence chart here

Hope that helps you.

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Dec 31, 2004
Posts: 961
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

Joined: Aug 27, 2004
Posts: 411

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

Joined: Oct 11, 2004
Posts: 3830
Thanks Jay ...
You remembered whole table for exam ...
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Oct 11, 2004
Posts: 3830

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

Joined: Aug 27, 2004
Posts: 411
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

Joined: Oct 11, 2004
Posts: 3830
Thanks a lot Jay , What about my previous question . It was really good one .
[ January 15, 2005: Message edited by: rathi ji ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
System.out.println(false && true | true); prints "false"

Mike Gershman
SCJP 1.4, SCWCD in process
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Aug 27, 2004
Posts: 411
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

Joined: Mar 13, 2004
Posts: 1272
| 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

Joined: Oct 11, 2004
Posts: 3830
No but , why the above class is not printing : I called
thanks a lot .
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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

Joined: Oct 11, 2004
Posts: 3830
It means that precedence is used only in grouping . execution always begin from left to right .

thanks a lot .
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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

Joined: Dec 31, 2004
Posts: 961
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

Joined: Oct 11, 2004
Posts: 3830

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

Joined: Dec 31, 2004
Posts: 961
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.

I agree. Here's the link: http://aspose.com/file-tools

subject: how to solve expression ...