File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operator Problem

 
Ritu Kapoor
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the output of the following program. Please tell me the reasons also.

Code
--------------------------------------------------------------------------
class Lang21 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}
}
---------------------------------------------------------------------------
 
David Ulicny
Ranch Hand
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
output:
true, false, false

||,&& are short circuits operators, so after || it will stop the evaluation.
 
Joseph Clark
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you duplicate the line . . .


. . . in the first line in the method main(), then you will get a clear picture of what is going on. By default, the class variables are false. The code does something that makes one class variable true.
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your answer is true, false, false


<b>The || operator evaluates its right-hand operand only
if the value of its left-hand operand is false</b>

But here the value on the left side is a true value

the statement <b>a=true</b> is making it true on left side...


I think, it will help u ...
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys! I got it.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic