File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which executes first? ++ Operator Question.

 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12020
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to me


You mean you have compiled and run some code to demonstrate what you say?
Please post it here.
 
victor kamat
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic