This week's book giveaway is in the Spring forum.We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
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
• Tim Cooke
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Liutauras Vilda
• Henry Wong
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Mikalai Zaikin
• Himai Minh

Question from Khalid's Book

Ranch Hand
Posts: 267
• Number of slices to send:
Optional 'thank-you' note:
This is a question from Khalid A.Mughal's book. I thought I knew the answer right but the right answer has left me confused
public class logic
{
public static void main(String[] args)
{
int i=0;
int j=0;
boolean t=true;
boolean r;
r=(t& 0<(i+=1));
r=(t&& 0<(i+=2));
r=(t |0<(j+=1));
r=(t | |0<(j+=2));

System.out.println(i +""+j);
}
}
What is the output.
The answer according to the book is 3 and 1.
I thought it would be 1 & 1. Can anybody explain this to me.

Ranch Hand
Posts: 464
• Number of slices to send:
Optional 'thank-you' note:
I got the i part . i is 3
i is changed two different places so it will be 3
But i am sure how the answer for j is 1?
Some one have any idea?

Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
I think I've got a fair measure of what this problem is about.
The operator | |, as we know, is a short circuit operator. It means that if the result of the operation can be determined by the left hand operand alone, then the right hand operand will be omitted.
In the case of the | | operator, the truth table is :

• True | | True = True
• True | | False = True
• False | | True = True
• False | | False = False

• In our case, the left hand operand of the fourth expression r=(t | | 0<(j+=2)); evaluates to a "True". Since "True" with any combination of True or False returns True for the | | operator, the system decides not to waste its resources in evaluating the Right Hand portion of the operator and just returns True. Consequently, the expression (0<(j+=2)) does not get evaluated and therefore the final value of j is 1.
Hope this helps
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 09, 2001).]

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?