• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Increment Operator question

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
K&B book page 291;

int x=2; int y=3;
if (y==x++ | x<++y)
...

Book says, "The first expression compares x and y and the result is false, because increment on x doesn't happen until after the == test is made"

OK. It is sensible.


But how about code at below.

public class Increment {
public static int getNum(int x) {
return x++;
}
public static void main(String[] args) {
System.out.println(getNum(44));
}
}

Please explain why the output is 44 instead of 45.
Any help will be appreciated.

Thanks,
Mehmet
 
author
Posts: 23928
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


But how about code at below.

Please explain why the output is 44 instead of 45.
Any help will be appreciated.



Can you please explain why it should be 45 instead of 44?

When the getNum() method calculates to value to be returned, it will use the value of x before it gets incremented (post increment means the value of x will be incremented after it has been used).

Sure, the value of x will be incremented, but the value to be returned has already been calculated by then.

Henry
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its more or less the same of assignment.

When you have



The above SOP will print the value as:



Why? and How? The value of x is being used in the operation first (ie., assignment). After the appropriate operation (here assignment) is done, the value of x is incremented! Right?

The same holds good in terms of your method as well. The value of x is being used for its operation (here, return value) first and then its incremented. But that does NOT mean that the updated value of x (incremented here) will be reflected back because the updation happens only after the value of x is being used (returned).

HtH.
[ July 09, 2007: Message edited by: Raghavan Muthu ]
 
Mehmet Gursul
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Henry and Raghavan.
"The value of x is being used for its operation (here, return value)"
is a good explanation.

Thanks again..
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome Mehmet Gursul

Its our pleasure!
 
reply
    Bookmark Topic Watch Topic
  • New Topic