• 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

postincrement operator ++

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my question is as follows,

public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
inc.fermin(i);
i = i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
ANS)1 Compile time error
2 Output of 2
3 Output of 1
4 Output of 0

The Correct Answer is
4) Output of 0

Explaination:
The method fermin only receives a copy of the variable i and any modifications to it are not reflected in the version in the calling method. The postincrement operator ++ effectively modifies the value of i after the initial value has been assigned to the left hand side of the equals operator. This can be a very tricky concept to understand

why does any modification done don't reflect the method?
and why the value assigned by the post increment don't affect i variable?

please kindly guide me, i have not understood the above topic.
thank you,
trupti
 
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

why does any modification done don't reflect the method?



It did, but the local variable went out of scope when the method ended. If you print the value prior to returning from the method, you will see that the parameter variable did change.

and why the value assigned by the post increment don't affect i variable?



This is a common question... that it is actually in the JavaRanch FAQ.

http://faq.javaranch.com/java/PostIncrementOperatorAndAssignment

Henry
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Try to use the code blocks, so that the indentation isn't lost and the code is clearer.
I'm not sure what you mean in your first question. If you're asking why the variable remains unchanged after calling fermin(), it is because the method only gets a copy of the variable you pass as argument. Once in the method, the parameter is much like a local variable, it has no effect on the outside world. If instead of a primitive the argument was an object, this would still be true, however the "object" is a reference to an object, and then the method would be handling the same actual object that is referenced outside the method. That is, the method takes only a copy of the reference, but this reference points to the same object that the reference passed by argument.

The i = i++ part is tricky indeed; I would have guessed the program outputs 1, but no, it prints 0 and I don't get it. The explanation says that the value of i actually changes after being assigned to the left operand, so...

Edit: Thanks Henry for the FAQ reference.
[ September 22, 2007: Message edited by: greg buela ]
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic