This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

increasing and decreasing in loop

 
Lao Kinsuyi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want someone to explain when to use ++x, --x, ++y, --y in for loop with examples and codes.

Secondly, look at this code :

class Uber{
static int y = 2;
Uber(int x) {this(); y = y*2;}
Uber() {y++; }
}
class Minor extends Uber{
Minor() {super(y); y = y+3; }
public static void main (String[] args) {
new Minor();
System.out.println(y);
}}
The answer given in the book of Kathy and Bert is 9.But, i think the answer should be 7. Because,y = 2 and y*2 is 4. With increment of 3 plus 4 i.e y+3(3+4) =7
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lao Kinsuyi:
...The answer given in the book of Kathy and Bert is 9.But, i think the answer should be 7. Because,y = 2...

But the first line of Uber(int) is this(), which calls Uber() to increment y to 3.

Try adding println statements to see what's happening...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic