Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes please answer this question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please answer this question." Watch "please answer this question." New topic
Author

please answer this question.

Naveen Megharaj
Ranch Hand

Joined: Feb 13, 2009
Posts: 39
int y=5;
int x=2;
if((x>3) && (y<2) || doStuff()) {
System.out.println("true");
}
else {
System.out.println("false");
}
}

//can anybody please explain how d above program segment gets executed if "doStuff()" method returns "true".
W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
should print true

edit: should probably post things like this in the beginner forums, just a thought


SCJP 6, SCWCD 5
Naveen Megharaj
Ranch Hand

Joined: Feb 13, 2009
Posts: 39
i know that it will print true, but i want to know how the "condition" with in the "if statement" simplifies.........
W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
x > 3 = false
y < 2 = false
doStuff() = true

so you probably already know

true AND true = true
true AND false = false
true OR true = true
true OR false = true

so basically your if statement breaks down to

as long as you have an OR that evaluates to true it will be evaluated as true

however something likewould evaluate to false. always work from inside parenthesis to outside. simplified this could be represented as and simplified still further would be
Hope that helps at all.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18759
    
  40

Naveen Megharaj wrote:i know that it will print true, but i want to know how the "condition" with in the "if statement" simplifies.........





A couple of points.... First, the logical AND has higher precedence than the logical OR, so the condition would be like this (if using parens)....



Second, the logical AND is a short circuit operator, so when the "x>3" portion is false, it will skip the check for the "y<2", and immediately return false.

Third, the logical OR is also a short circuit operator, but can't short circuit because the left side is false. It will call the doStuff() method which returns true -- and generates a logical OR result of true.

Hence, the condition in the "if" statement is true...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Naveen Megharaj
Ranch Hand

Joined: Feb 13, 2009
Posts: 39
thats it............!!! thanks a lot to all the repliers.......it was very helpful.......please continue the good work.
once again thanks.....
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I don't see the actual doStuff() method here, but it must be a Boolean method right? How can we say it returns true if we don't know the method?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18759
    
  40

Fred Hamilton wrote:I don't see the actual doStuff() method here, but it must be a Boolean method right? How can we say it returns true if we don't know the method?


This was stated in the original post -- that the doStuff() method returns true.

Henry
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Henry Wong wrote:
Fred Hamilton wrote:I don't see the actual doStuff() method here, but it must be a Boolean method right? How can we say it returns true if we don't know the method?


This was stated in the original post -- that the doStuff() method returns true.

Henry


ahh right you are. I had not picked up on that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
But if the doStuff() method doesn't return a boolean, the compiler will pick up a discrepancy between the || operator and the return type, and refuse to compile your code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please answer this question.