• 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

UNexpected Output

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UNexpected Output
Wats the output to this??
public class Test {
public static void main(String argv[]) {
Test inc = new Test();
int i =0;
inc.fermin(i);
i = i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}

I m getting 0 but i dont have any clue to this..???
i m using i= i++;
It should increment i to 1..as far as i know....
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,
Java is a Stack Based Language
i=i++;

so what happens here is that

1) i value is stored in top of stack
2) Now the valued in top of stack is pushed backed to the variable that is old value itself
3) Now a new valued in created incementing it in another location(not the value in top of the stack is not effected)


This is differnt in other languages like C, C++.(I hope that made u confused).

so have the value incremented u have to use

i = ++i;

so what happens here is that

1) i value is stored in top of stack
2) Now a new valued in created incementing it in same location(note that value in top of the stack is effected)
3) Now the valued in top of stack is pushed backed to the variable that is new inremented value


KINDLY POINT OUT IF AM WRONG AT ANY POINT

regs
Vivek K Nidhi
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i = i++;
Evaluate the right-hand operand of =, the expression i++:
1. The value of the expression i++ is the value of the variable i, 0.
2. Then increment the variable i. i == 1
Perform =
1. Assign the value of the right-hand operand to the variable i. i == 0.
[ December 10, 2003: Message edited by: Marlene Miller ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic