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?
I love MySQL<BR>Sun Certified Programmer for Java 2 Platform
Peng Cedar
Greenhorn
Joined: Jul 25, 2001
Posts: 10
posted
0
Code 1:
I got : 1 Code 2:
I got : 0
Hassan Naqvi
Ranch Hand
Joined: May 03, 2001
Posts: 158
posted
0
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.
Always Belive On Logic!!
Eric Pramono
Ranch Hand
Joined: Jul 09, 2001
Posts: 74
posted
0
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
Joined: May 03, 2001
Posts: 158
posted
0
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.
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
posted
0
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
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
posted
0
sorry !!! just to put on top.... Thanks in adv. ------------------ Regards Ravish