Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is this??? I m confused Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is this??? I m confused" Watch "what is this??? I m confused" New topic
Author

what is this??? I m confused

atif aslam
Greenhorn

Joined: Sep 17, 2008
Posts: 12
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

atif aslam
Greenhorn

Joined: Sep 17, 2008
Posts: 12
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;
}

}

output:
a
b
c
fail

But what happended in code 1? can sumbody exlain?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

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 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
atif aslam
Greenhorn

Joined: Sep 17, 2008
Posts: 12
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...


I tried it myself.
If somebody had posted this question elsewhere then i dont know.
If not then source is mine
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797


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
atif aslam
Greenhorn

Joined: Sep 17, 2008
Posts: 12
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
Ankush Baveja wrote:This is actually my first post, so hello to all

output:
a
b
c
pass



output shows what m talking about
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

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

Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

Yes Ankush
Rohan Amin
Greenhorn

Joined: Mar 19, 2008
Posts: 15
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
This is a similar kind of example from Devaka's Exam....


OUTPUT : Sub1 2
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Ankush,

Please use code tags and indent your code properly.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8875
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
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:


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

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
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
atif aslam
Greenhorn

Joined: Sep 17, 2008
Posts: 12
Treimin Clark wrote:Ankush,

Please use code tags and indent your code properly.

sure i will
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is this??? I m confused