Win a copy of Murach's MySQL this week in the JDBC and Relational Databases 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operators and Assignments??

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic