*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes value of i? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "value of i?" Watch "value of i?" New topic
Author

value of i?

Swati Kadam
Ranch Hand

Joined: Mar 05, 2008
Posts: 39
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
}
};


SCJP / Preparing For SCWCD
Andriy Fedotov
Ranch Hand

Joined: Mar 07, 2008
Posts: 49
yep, that's really funny..
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
interesting ...can somebody explain please
Padmanabh Sahasrabudhe
Ranch Hand

Joined: Mar 04, 2008
Posts: 53
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

Joined: Feb 28, 2007
Posts: 274
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

Joined: Mar 02, 2008
Posts: 8
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

Joined: Feb 24, 2008
Posts: 49
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

Joined: Nov 05, 2007
Posts: 661
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 ]

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Uttara Rishi
Ranch Hand

Joined: Feb 24, 2008
Posts: 49
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

Joined: Feb 24, 2008
Posts: 49
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

Joined: Oct 15, 2007
Posts: 71
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

Joined: Feb 24, 2008
Posts: 49
Thanks Kesava.That helped.


Uttara Rishi.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

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 ]

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"Java Developer",
Please check your private messages.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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

Joined: Feb 24, 2008
Posts: 49
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

Joined: Mar 11, 2008
Posts: 25
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 ]

SCJP 5 with 94%
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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

Joined: Feb 28, 2007
Posts: 274
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

Joined: Nov 05, 2007
Posts: 661
Thanks alot,Gitesh !!

When you use too much of your Brains, you forget the Basics
Take Care.
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Welcome Nabila
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: value of i?
 
Similar Threads
Anonymous class behavior confusion !
If conditional not branching correctly
WA #2 ..... word association
What if i want a return
what is the error in this program