• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

i=i++?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code 1:
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
int j;
inc.fermin(i);
i = i;
i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
I got outprint: 1
Code 2:
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
int j;
inc.fermin(i);
i = i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
I got outprint: 0
Why?Who knows?
 
Peng Cedar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code 1:

I got : 1
Code 2:

I got : 0
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the above code you are sending value of i as an argument to the method fermin() in Line 1.Always remember when you send any primitive value like this, then always the copy of that value is send to method.This is called pass by value.
Now, copy of i(i.e 0 in this case) is send to fermin() at Line 1 which is copied in parameteri of fermin() at Line 4.Remeber this i is the local variable of method fermin().
At Line 5 you have increment the value of i.Infact you have incremented local variable of method fermin()
This increment has no effect in the value of i passed at Line 1.Now the story ends.And after executing the method fermin the control comes back to Line 2 and 0 is assinged to i.And hence incremented 1 at Line 3.
Hope this help.
Regards,
Hassan.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peng,
Consider the following code:

It will print:
9
10
10
12
Please note line marked as line 1 & line 2..
The statement i=i++ is considered by the compiler as a simple storing an integer value from i. Because the increment process is done after storing the value.. it doesn't affect the value of i.
This is different from the statement i=++i, where the increment process occurs first before storing the value.
In line 2, the compiler will increment value of i and storing it.. all before printing it.. so that's why i value gets incremented to 11, and will be printed as 12 within the next statement..
hope this helps..
- eric
 
Hassan Naqvi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Well, i am not going to discuss the whole code above .Infact i will explain the special case of Increment/Decrement operator at Line 1 i.e i=i++;.
Before going in the crux of matter,have a look on some some rules of Java for Increment/Decrement operator.
We have two cases in Increment/Decrement operator.
1:Postfix
2:Prefix
Note:The first rule is common for both cases.
Rules
1: The value substituted by Java/Compiler is always assinged.
2: In postfix case, Java/Compiler first substitute the value & then increment/decrement.
3:In prefix case, Java/Compiler first increment/decrement the value & then substitute.

By applying the above rules let us solve the special case

i=i++;

Consider the following segment of code,

i=0; //Line 1
i=i++; //Line 2
S.O.P(i); //Line 3


This is postfix case,therefore applying the second rule.
Firts substitute the value i.e i is substituted by 0 at Line 2.
Then it is incremented(Remeber this increment is made in memory).
Now applying first rule(which is common for both cases).
i.e the value substituted is always assinged.Therefore the value substituted is 0 at Line 2 will be finally assinged to i.
And hence the value printed in Line 3 will be 0.
Hope this help.
Regards,
Hassan.
 
Ranch Hand
Posts: 5415
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hassan Naqvi:
[B]

rules of Java for Increment/Decrement operator.
We have two cases in Increment/Decrement operator.
1:Postfix
2:Prefix
Note:The first rule is common for both cases.
Rules
1: The value substituted by Java/Compiler is always assinged.
2: In postfix case, Java/Compiler first substitute the value & then increment/decrement.
3:In prefix case, Java/Compiler first increment/decrement the value & then substitute.

By applying the above rules let us solve the special case
i=i++;

Consider the following segment of code,

i=0; //Line 1
i=i++; //Line 2
S.O.P(i); //Line 3


This is postfix case,therefore applying the second rule.
Firts substitute the value i.e i is substituted by 0 at Line 2.
Then it is incremented(Remeber this increment is made in memory).
Now applying first rule(which is common for both cases).
i.e the value substituted is always assinged.Therefore the value substituted is 0 at Line 2 will be finally assinged to i.
And hence the value printed in Line 3 will be 0.
Hope this help.
Regards,
Hassan.
[/B]


it is incremented(Remeber this increment is made in memory)
what happens to that incremented value ??
if I write:
j = i++;
I break it up as
j = i;
i = i + 1;
from your statement what I get is this:
temp = i ;
temp = temp + 1 ;//(Remeber this increment is made in memory)
//i = temp; // if i assign temp to i here then j will be assigned incremented value OR I take two temp var. Can any1 break it like this.Tkx in adv.
j = i;
i = temp;

In this scenario still I should get incremented value for i=i++.
Thanks in advance
------------------
Regards
Ravish
 
R K Singh
Ranch Hand
Posts: 5415
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry !!!
just to put on top....
Thanks in adv.
------------------
Regards
Ravish
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic