SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:Hi Ankush! Welcome to javaranch.
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...
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
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
Ankush Baveja wrote:This is actually my first post, so hello to all
output:
a
b
c
pass
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
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"
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]
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
hi ankush this is teja in boolean conditional expression evaluation the com[iler treats the above a()|b()&c() expressionAnkush 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:
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;
}
}
output:
a
b
c
pass
SCJP 5
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