Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Operators and Assignments??  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.
1. public class ShortCkt {
2.public static void main(String args[]) {
3.int i = 0;
4.boolean t = true;
5.boolean f = false, b;
6.b = (t && ((i++) == 0));
7.b = (f && ((i+=2) > 0));
8.System.out.println(i);
9.}
10. }

A)0
B)1
C)2
D)3
why the answer is b? and why (line 7) second assignment to variable b, the expression (i+=2) does not get evaluated? please explain, thanks..
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an example of the short-circuit operation of the && operator.

b = (f && ((i+=2) > 0));

At this point, variable f has the value false. The operator is a logical AND (&&). The result of false AND true or false AND false is : false.
So the statement does not need to be evaluated completely to know that b will be assigned the value false. The rest of the statement after && is ingored, so the side-effect of the i increment is never performed.
Rob
 
leow
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rob, what if the && change to & or |, the statement will be evaluated completely?
b = (f & ((i+=2) > 0)); or
b = (f | ((i+=2) > 0));
thanks..
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in that case both will be evaluated
/SAmith
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by leow:
thanks Rob, what if the && change to & or |, the statement will be evaluated completely?
b = (f & ((i+=2) > 0)); or
b = (f | ((i+=2) > 0));
thanks..


sorry i'll explain in more detail
the AND (&) opertator results in TRUe id both the operand are True and the OR operator (|) results in TRUE if either one of the operands are TRUE.
unlike the Logical AND , OR the Boolean AND, OR check BOTH the operands to get the result.
so in your example both the operands will be evaluated.
/SAmith
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!