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 Which executes first? ++ Operator Question. 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 "Which executes first? ++ Operator Question." Watch "Which executes first? ++ Operator Question." New topic
Author

Which executes first? ++ Operator Question.

Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152


In the previous code, what exactly happens?.

1. Read the instance variable counter.
2. Return counter.
3. Increment counter by one.


or

1. Read the instance variable counter.
2. Increment counter by one.
3. Return counter.
















-----------------------------------------------
According to me it first increments the counter by 1 and then returns. But according to Paul Sanghera and his team, the method returns first and then increment.

What's according to you?


SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

read value of counter, and remember it for a moment.

increment value of counter

return the stored value of counter

forget what that stored value was
[ March 01, 2007: Message edited by: Fred Rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
According to me


You mean you have compiled and run some code to demonstrate what you say?
Please post it here.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
I wrote code to test and Sanghera is right.

It is interesting to note that if the code is

int counter = 10;
return counter+=10;

then 20 is returned.
Quintin Stephenson
Ranch Hand

Joined: Nov 16, 2006
Posts: 40
Paul Sanghera and his team would be right.
The answer is:
1. Read the instance variable counter.
2. Return counter.
3. Increment counter by one.

A '++' after the variable mean give me the value and then increment its value. If the '++' was before the variable, then you would be correct.

However consider a variation of this theme using for loops with the following code:

public class test2 {
static int counter = 0;
public static void main(String[] args) {
for (;counter < 9;++counter)
System.out.print(counter+",");
}
}

Why is the result
0,1,2,3,4,5,6,7,8


If at first you don't succeed, try, try again. If you don't try you have failed.
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
quote:According to me



You mean you have compiled and run some code to demonstrate what you say?
Please post it here.


When I say According to me.... I don't pretend to demonstrate anything because if someone could demonstrate something, I would be true. I'm asking because I'm not sure. And of course I always try to write code before posting in this forum.

On the other hand I don't know how to test this, maybe with threads, I dunno... , but I remember when you see something like this x++ + x; it increments x just after reading x and talking into account for the operation and not in the following line after semicolon ; , but I am not sure what happens when returns are involved.

victor, how did you test it?
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
However consider a variation of this theme using for loops with the following code:


Because it goes from 0 to 8 and nothing special happens.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

On the other hand I don't know how to test this, maybe with threads, I dunno... , but I remember when you see something like this x++ + x; it increments x just after reading x and talking into account for the operation and not in the following line after semicolon ; , but I am not sure what happens when returns are involved.


What do you need threads? You just need to check if the value returned is the value before or after the increment. Try...



Henry
[ March 01, 2007: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
Henry, I have seen my big mistake, and this is a very easy question. But I still see something there, maybe in the way Java is implemented.

I was tricked by the idea that once a method returns nothing can happen inside it, then how could a variable be incremented once a method returns?. Of course I understood the lesson, but maybe there is a stack or something, I don't know.

By the way, it is a honor to be listened by a book author and over 1000 comments in this forum.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Which executes first? ++ Operator Question.