File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes increasing and decreasing in loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "increasing and decreasing in loop" Watch "increasing and decreasing in loop" New topic
Author

increasing and decreasing in loop

Lao Kinsuyi
Ranch Hand

Joined: Mar 22, 2007
Posts: 30
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

Joined: Aug 31, 2004
Posts: 11343

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...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: increasing and decreasing in loop
 
Similar Threads
Doubt in Constructor
Doubt in K&B SCJP 5: Chap 2, SELF TEST, Q 11
question about static members
Class using Main without declaring itself Public
master exam question