• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to solve expression ...

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .

please help ...

thanks a lot .
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rathi,
Refer to operator precedence chart here
and see if you can get the answer as 5, if not I will be glad to help you.

Hope that helps you.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ?


Ok... this table will help you.

click me
[ January 14, 2005: Message edited by: Jay Pawar ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jay ...
You remembered whole table for exam ...
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ...

please help ...

thanks a lot .
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(false && true | true); prints "false"
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...

please help ...

thanks a lot .
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering the following operator precedence given in Thinking in Java by Bruce Eckel.


OPERATOR PRECEDENCE

Ulcer Unary operators (),[],new, ++,--(postfix)
++,--,+,-,~,! (prefix)
Addicts Arithmetic *,/,%
+,-
<<,>>,>>>
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
| 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: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No but , why the above class is not printing : I called
please help .
thanks a lot .
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that precedence is used only in grouping . execution always begin from left to right .

please comment .
thanks a lot .
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic