• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which executes first? ++ Operator Question.

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic