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

javaprepare question

 
geetha nagarajan
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t || ((i++) == 0));
b = (f || ((i+=2) > 0));
System.out.println(i);
}
}
The answer acc.to javaprepare is 2.
I think it's 3.
Pl.help.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it? I'll bet that will tell you the correct answer.
Corey
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The conditional or || doesn't evalute the right operand "(i++) == 0)" if the left one "t" is true. Thus 2 is printed.
Similarly, && doesn't evalute the right operand if the left is false.
This is done for the sake of speed.
 
geetha nagarajan
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,i tried it out.The o/p is 2.
After the statement: b=t||((i++)==0));
The value of i is 0,not 1.
Why is it so?That's my confusion.
 
geetha nagarajan
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jose,for the clarification.
Got it.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by geetha nagarajan:
After the statement: b=t||((i++)==0));
The value of i is 0,not 1.
Why is it so?That's my confusion.

That would have been a great question to start with.
 
Lloyd Wilson
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The operation within the if () clause does not proceed testing an operation if it first meets a condition that equates to a true statement.
If reading 'if (true, ((i++) == 0))' the condition starts checking the left side of the operation which validates to true. Because the boolean is true, it continues performing the operation within the if block and doesn't check the right side of the condition. The '||' operation only looks for a true result first, and if successful (true), performs no other operations (meaning, the right hand side does not get looked at).
In the case of 'if (false, ((i+=2) > 0))' the condition that is first checked is false, which in the case of an '||' or operation, it looks to find a condition that is equal to true, and in this case, the operation i+=2 > 0 is performed.
This only works because the '||' condition on the left first produced a false result. The condition on the right executes because the '||' condition found no true statement.
In your response, if the condition of the boolean in the first check was false, you would get the value of '3'.
Execute the code and play around to see how it works.
Cheers,
Lloyd Wilson
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic