• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

value of i?

 
Swati Kadam
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class TrickyTest {

public static void main ( String args [ ] ) {
int i = 10 ; // line 1
i = ++i ; // line 2
i = i++ ; // line 3
System . out . println ( i ); // line 4
}
};
 
Andriy Fedotov
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep, that's really funny..
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interesting ...can somebody explain please
 
Padmanabh Sahasrabudhe
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i = 10 ; // line 1 -- i = 10
i = ++i ; // line 2
i is incremented by 1 and then assigned to i. so the value becomes 11
i = i++ ; // line 3
i's value is first taken for assignment and stored in memory. then its value is increase by 1. so the current value is 12. but here the statement execution is not complete. the value 12 gets overwritten with the assignment of the value stored in memory. hence it regains the value of 11.

a little change in code given below will demonstrate it. here the last step gets omitted and hence you get i=12.

class TrickyTest {

public static void main ( String args [ ] ) {
int i = 10 ; // line 1
int a = 0;
i = ++i ; // line 2
a = i++ ; // line 3
System . out . println ( i ); // line 4
}
}
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Swati Kadam:
class TrickyTest {

public static void main ( String args [ ] ) {
int i = 10 ; // line 1
i = ++i ; // line 2
i = i++ ; // line 3
System . out . println ( i ); // line 4
}
};


@line1-- i=10
@line2-- i=(10+1)=11
@line3-- i=i;//means i is still 10
@line4-- prints 10

Some great links to visit for deep information:
http://radio.javaranch.com/corey/2004/06/03/1086277171000.html
http://www.coderanch.com/t/244938/java-programmer-SCJP/certification/post-increment-confusion
http://faq.javaranch.com/java/PostIncrementOperatorAndAssignment

Regards,
Gitesh
 
Keith Hennessy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the value is 11. I just compiled and ran the code and it produced 11.

int i = 10;
i = ++i; // Sets i = 11;
i = i++; // Sets i = 11 and then increments i;
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added a line



Output: Value of i = 11
Value of i = 11

Why hasnt it incremented the value of i to 12?
Can anybody please explain?

Thanks in advance.
Uttara Rishi.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read out Padmanabh's answer.
It is mentioned very clearly.

In the expression ,

i=i++;

The value of i which is 11 in the previus expression is getting Stored first , and it is then being Incremented.
So the answer which is stored, is the value being displayed - which is 11.
[ March 10, 2008: Message edited by: Nabila Mohammad ]
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Padmanabh and Nabila . Sorry for overlooking it.

I think internally this is what is happening.



Please correct me if I am wrong.

Uttara Rishi.
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

Here is anothe tricky code.




Output : 1,0

I have a doubt in line 1. I think f1(0) should be passed, but obviously it isnt.

Also consider this code.


Output: 5.
A per my deduction it is 1++ + 2++
2 + 2 = 4 // now a= 4
Hence a+=4 // a = 4 + 4 = 8.
Which is obviously not correct.
Can somebody please shed some light on this? I would really appreciate it.

Thanks in advance.

Uttara Rishi.

[ March 10, 2008: Message edited by: Uttara Rishi ]
[ March 10, 2008: Message edited by: Uttara Rishi ]
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi the following line is evaluated as



a += ++a + a++;
a = (int) ((a) + (++a + a++)) ------> a = 1 + (++a + a++))
a = (int) ((a) + ((++a) + (a++)))-----> a = 1 + (2+2) = 5.
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kesava.That helped.


Uttara Rishi.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class TrickyTest {

public static void main ( String args [ ] ) {
int i = 10 ; // line 1
i = ++i ; // line 2
i = i++ ; // line 3
System . out . println ( i ); // line 4
}
};
The output is 11

Step by step explanation is as follows:

at line 1 integer i is declared(local variable i) and initialize to 10

at line 2 first value of i will get increament to 11 and then assignment happens (pre increament rule see JLS(java language specification) third edition)

at line 3 first value of i which is 11 initially get saved in to temparary variable and then value of i got increamented.
After increament the initial value of i which is stored in temparary variable and is used for assignment.
Finally 11 is assigned to i.(see JLS)

example : see the code given below
public class Test{
public static void main(String args[]){
int j = 0;
for(int i = 0; i<100; i++){
j=j++;
}
System.out.println(j);
}
Output is always 0
because initial value of j is 0 which is stored in to temparary variable then value of j is increament by 1 and finally value of temparary variable is assigned to variable j and value of variable is always 0.

[ March 11, 2008: Message edited by: Java Developer ]
[ March 11, 2008: Message edited by: Ninad Kulkarni ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Java Developer",
Please check your private messages.
-Ben
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally when I thought i got the Increment operator, I realised I didn't.


Can some one explain why the out put is always 0.
I thought the stored value is 0 and in the next counter it would get be 1 (after assignment) but in this case, it displays 0.
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nabila,
Here's the explanation.


int i = 10;
i = i++;
Internally:
temp = i // temp = 10;
i = i+ 1 // i = 11;
i = temp // i = 10;

whereas when
i = ++i;
temp = i // temp = 10;
i = i + 1 // i = 11;
i = i // i = 11;

Hope this clears it up.

Thanks Ninad.

Uttara Rishi.
[ March 12, 2008: Message edited by: Uttara Rishi ]
 
Mukesh Bhojwani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nabila,




As it is post increment ( j=j++ ),
so internally it will execute like this

temp = j;
j = j + 1;
j = temp;


See the Dry run of the loop

i = 0
-----
temp = j;// 0
j = j + 1;// 1
j = temp;// 0

i = 1
-----
temp = j;// 0
j = j + 1;// 1
j = temp;// 0

i = 2
-----
temp = j;// 0
j = j + 1;// 1
j = temp;// 0

I hope you understood it, & I need not to write it up to i = 9, as even if the loop executed 100 times won't make change to the value of 'j'

Also one more thing, if the code is changed a bit [Pre increment]



As it is pre increment ( j=++j ),
so internally it will execute like this

j = j + 1;
temp = j;
j = temp;

See the Dry run of the loop

i = 0
-----
j = j + 1;// 1
temp = j// 1
j = temp// 1

i = 1
-----
j = j + 1;// 2
temp = j// 2
j = temp// 2

i = 2
-----
j = j + 1;// 3
temp = j// 3
j = temp// 3

So After completing entire loop the value in the j will be 10


thanks,
Mukesh
[ March 13, 2008: Message edited by: Mukesh Bhojwani ]
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Mukesh and Uttara .
That was a big help.

Ok..
Let's consider this case :


In this case , in the first statement

x=4.5

x++
temp=4.5
x=x+1
x=temp // x=4.5

And then
++x
So x=x+1
temp=x
x=temp//x=5.5

And if you add the two, you get 10 when you are supposed to get 11.
I thought the temp value stored is the one displayed, but internally the value is incremented.So incase you have another expression using that variable then it will use the incremented value and not the value stored in the temporary variable.

So what I figured in (System.out.println(x++ + ++x)

x++-> temp value is 4.5 but incremented value is 5.5 (After storing the temp varible)

So in ++x
It takes the Incremented Value 5.5 and then Increments it to 6.5/

Finally)
4.5+6.5=11.0


So where am I going wrong...???

And thanks for your time and patience!!!
[ March 13, 2008: Message edited by: Nabila Mohammad ]
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nabila,

I see no confusion.

Well i modified the code a bit to break down the problem, to have a clear and concrete view.

For the following line:


The modified code is as follows:



In short it is calculating as : 4.5 + 6.5=11.0

Hope that clears the confusion.

Regards,
Gitesh
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot,Gitesh !!

When you use too much of your Brains, you forget the Basics
Take Care.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome Nabila
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic