aspose file tools*
The moose likes Beginning Java and the fly likes Simple If statement syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple If statement syntax " Watch "Simple If statement syntax " New topic
Author

Simple If statement syntax

Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Hi Friends,
Just wondering...Is this a legal if statement:
[code]
if(hand<50 & !b) hand++;
[code]
Don't we need to have '&&' instead of '&' here.Please answer if you know this.Thanks


SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
That is a legal if statement. You're using the non-short-circuited boolean operator, which means that both sides of the & always get executed, regardless of the result of evaluating the left hand side.

-Jeff-


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Yes, it is perfectly legal to do a "bitwise and" operation with two boolean values, and the result is also a boolean.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Giovanni De Stefano
Ranch Hand

Joined: Aug 17, 2004
Posts: 144
Hi jas,
in Java there are short circuit logical operators &&, || and non short circuit operators like &, |.
The non short circuit operators ALWAYS evaluate both sides of the expression (though they are inefficient) while the short circuit operators don't waste time evaluating useless part of the expression.
An example will make everything clear:
a = 0;
b = 1;
c = d = 2;
if (a==b && c==d)...
at runtime this is what happens: a is equal to b ? NO...the entire expression is false, because there is AND (&&) so don't even bother evaluating the second part of the expression (c==d) because the result is FALSE regardless of the value of the second expression

If you had & instead of &&:
a = 0;
b = 1;
c = d = 2;
if (a==b && c==d)...
at runtime this is what happens: a is equal to b ? NO...LET ME CHECK the other expression anyway...c is equal to d ? YES...but FALSE AND TRUE is FALSE...I wasted my time evaluating the second expression even if its value would have not changed the FINAL result! That's why non circuit operator are inefficient.

The same thing happens with || and |, with || if the first expression is TRUE, the FINAL result is TRUE, regardless of the value of the second expression; but if you use |, then even if the first expression is TRUE, the second expression is still being evaluated.

After this preface, yes...the code you wrote is correct and both hand<50 AND !b are evaluated.

I can provide you some practical examples of why you should use short circuit operators, but I don't want you to get confused.

Let me know if this explanation is enough!
Giovanni


SCJP 1.4
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1010
    
    3
I've been waiting YEARS for someone to ask this.

When used between boolean operands, & and | act just like && and ||, respectively, except that the single-symbol ones will always evaluate both operaands.

So something like...

...will print...
Before: a=2, b=3
After: a=2, b=3

...becasue once the a>5 evaluates to false, the b++>5 isn't evaluated.

However,...

...will print...
Before: a=2, b=3
After: a=2, b=4

Even though a>5 is false, the single & causes the b++>5 to be checked.

Ryan
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1010
    
    3
DAMN! Three people answered while I was typing MY response.

Henry, be careful calling & and | between boolean operands "bitwise operators". That fails to point out the whole non-short-circuit aspect of them.

Ryan
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Originally posted by Giovanni De Stefano:
I can provide you some practical examples of why you should use short circuit operators, but I don't want you to get confused.


And in fact, you should prefer the use of short-circuited operators. The deliberate use of non-short-circuited operators means that you require the right-hand side to always be evaluated. That's potentially a design flaw--see http://c2.com/cgi/wiki?CommandQuerySeparation for a bit more information. There may be some cases (see the note about Kevlin's article) where you're forced to use non-short-circuited operators, but in general you should avoid them.

-Jeff-
[ April 29, 2005: Message edited by: Jeff Langr ]
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Thanks EverBody,
I really appreciate to get so much response in a matter of few seconds.And I would really like to thank you all guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple If statement syntax