• 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

Incremeant - Marcus Greens Exam-3, Q-54

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
inc.fermin(i);
System.out.println(
i = i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
Can any body tell me how output is 0. I think after execution of (i = i++) i must be 1.
Rehan.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's a discussion
http://forum.java.sun.com/read/16789542/qAa6ifNJoxK4AAotL#LR
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i=i++; will be 0 since first the post increment operator(++) on i is evaluated which after the statement would have changed the value to 1 and then the assignment of old value of i to i is done keeping it 0;
hence i maintains the value 0;
 
rehan hamid
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I got it!!
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but in this case, the method "fermin" is called on i first...incrementing it. So shouldn't the output be 1?
1. first i = 0
2. inc.fermin(i); causes i to increment to 1
3. i=i++ still keeps i at one when printed
If the method were not called, then i would be 0, but it is...
I am confused. I will compile and run the code to see what happens...
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Output is indeed
0
0
This confuses me, since the method fermin(i) is called before the print statements...
[This message has been edited by ryan burgdorfer (edited March 02, 2001).]
[This message has been edited by ryan burgdorfer (edited March 02, 2001).]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method fermin increment the formal paramter i not the local variable declared i in main. The scope of the formal parameter is the entire body of the method. Outside the scope it does not exist.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ryan burgdorfer:
1) we are declaring and initializing a Local variable int i to zero.
2) Then we are passing that i to a method, which is incrementing, that i. Here we should understand that copy of value is passed so the original variable remain unchanged.
3) Then we print the result of the expression I=I++; that is zero.

4)At the last we are printin the value of the I which is zero
Hope this helps.
Siva

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well put Siva! It would be different if the fermin method had a return, like so:
import java.io.*;
import UU.*;
// increment test
public class Inc
{
public static void main( String[] args )
{
Inc inc = new Inc() ;
int i = 0 ;
i = inc.fermin( i ) ;

System.out.println( i = i++ ) ;
System.out.println( i ) ;
}

int fermin( int i )
{
i++ ;
return i ;
}
}
Output:
1
1
Process Exit...
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<*LIGHT BULB*>
Thanks guys for the enlightenment.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the answer I give to that question.. (by the way can anyone confirm if you are likely to get a question on the way var++ works?).

"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 post increment operator ++ effectivly modifes the value of i after the initial value has
been assiged to the left hand side of the equals operator. This can be a very tricky conept to understand"
(hmm just noticed that spelling error conept better fix it).
Marcus
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic