This is actually my first post, so hello to all
I m preparing for SCJP 1.6, and i was going all fine until i tried this question
can sumbody explain me the solution
code 1:
But when i changed the condition to a|b&&c it gave me a different answer, which actually makes sense
code 2:
public class prac1 {
public static void main(String [] args){
if(a()|b()&&c())
System.out.println("pass");
else System.out.println("fail");
}
static boolean b(){
System.out.println("b");
return true;
}
static boolean a(){
System.out.println("a");
return true;
}
static boolean c(){
System.out.println("c");
return false;
}
Ankush we have a rule here that when you post a question, you have to QuoteYourSources. So please tell us from where did you get this question so that we can help you...
Ankush we have a rule here that when you post a question, you have to QuoteYourSources. So please tell us from where did you get this question so that we can help you...
I tried it myself.
If somebody had posted this question elsewhere then i dont know. If not then source is mine
In First case if statement evaluates to following order:
if( true | (true&false) )
In Second case if statement evaluates to following order:
if( (true|true) && (false) )
I hope this clears
and see what other will say about it
[Reason for Editing of text : extra "|" in second if at the end of true keyword deleted]
Ninad Kulkarni wrote: In First case if statement evaluates to following order:
if( true | (true&false) )
In Second case if statement evaluates to following order:
if( (true|true|) && (false) )
I hope this clears
and see what other will say about it
when it evaluates the condition (a|b&c)
it gives the output a,b,c so it follows left-right associativity
and no precedence
atif aslam
Greenhorn
Joined: Sep 17, 2008
Posts: 12
posted
0
Ankush Baveja wrote:This is actually my first post, so hello to all
As per my knowledge in java expression evaluates from left to right and also operator precedence matters a lot
so don't think "no operator precedence"
atif aslam
Greenhorn
Joined: Sep 17, 2008
Posts: 12
posted
0
Ninad Kulkarni wrote:As per my knowledge in java expression evaluates from left to right and also operator precedence matters a lot
so don't think "no operator precedence"
yeah i know it matters
i was just reffering to the problem in hand
atif aslam
Greenhorn
Joined: Sep 17, 2008
Posts: 12
posted
0
Ninad Kulkarni wrote: In First case if statement evaluates to following order:
if( true | (true&false) )
In Second case if statement evaluates to following order:
if( (true|true) && (false) )
I hope this clears
and see what other will say about it
[Reason for Editing of text : extra "|" in second if at the end of true keyword deleted]
I think you are right. i was confused a bit on the order of execution of the functions
i thought operands with high precendence operators are evaluated first .
Use a debugger and you will see that the functions are evaluated first. So it prints out a,b,c and then it evaluates the if condition which by the way has
(true|true&false). Referring to operator precedence true & false is false and true | false is true so it prints out pass.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
Balaji Bang
Ranch Hand
Joined: Apr 23, 2007
Posts: 180
posted
0
This is a similar kind of example from Devaka's Exam....
on the SCJP 6 exam you will need to understand logical operators (short circuit and not), but you will NOT have to remember the kinds of precedence rules being discussed in this thread. In the older exams there were precedence questions like this, but these have been removed.
hth,
Bert
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
teja dharma
Ranch Hand
Joined: Feb 07, 2009
Posts: 51
posted
0
Ankush Baveja wrote:This is actually my first post, so hello to all
I m preparing for SCJP 1.6, and i was going all fine until i tried this question
can sumbody explain me the solution
code 1:
hi ankush this is teja in boolean conditional expression evaluation the com[iler treats the above a()|b()&c() expression
aV(b^c)=(aVb)^(aVc)=(t)^(t)=t
the other expression a()|b()&&c() can be treated as (aVb)^c=(a^c)V(b^c)=(f)V(f)=f
SCJP 5
atif aslam
Greenhorn
Joined: Sep 17, 2008
Posts: 12
posted
0
Rohan Amin wrote:Use a debugger and you will see that the functions are evaluated first. So it prints out a,b,c and then it evaluates the if condition which by the way has
(true|true&false). Referring to operator precedence true & false is false and true | false is true so it prints out pass.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
Thanks for the usefull suggestion, i dont have a debugger can you suggest one. M using eclipse