File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how the assignment happened 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 "how the assignment happened" Watch "how the assignment happened" New topic

how the assignment happened

Michael Lin
Ranch Hand

Joined: Dec 04, 2000
Posts: 31
public class Arg{
public static void main(String argv[]){
Arg inc = new Arg();
int i =0;
i = i++;
System.out.println(i); //line 1, 0???

int k =0;
System.out.println(k++); //0
System.out.println(k); //line3, 1
//hi, my confusion is why the line 1 still give 0?
//after i= i++, the i should be 1 now even if it is post increment.
//it should like the same in the line 3.
Someone please explain to me, thanks!
rajani peddi
Ranch Hand

Joined: Nov 27, 2000
Posts: 73
Michael Lin,
int i =0; // line 1
i = i++; // line 2

There are 3 steps involved in this :
1. First in line2 i is assigned 0 (cos of line 1, and i++ is a postincrement operator)
2. Then i++ is evaluated ( so i = 1)
3. Now i = 0 is evaluated ( from step 1)
Hope this clears
vadiraj vd
Ranch Hand

Joined: Dec 15, 2000
Posts: 68
Hi micheal,
I have he same doubt that how the assignment is happening.
I tried this which is the equivalent of your code.
public class StrangeAssignment
public static void main(String a[])
int i = 0;
i = i = i+1; // i++ is equivalent to i=i+1.
But the answer is 1 !!. Really it is a strange assignment.
I tried the same in C/C++ it's giving 1.

Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
ok lets take this expression
i = i++ + i
what is the answer .. its one..
what it did was
it writes the value of i before the + sign ( i=0) which is zero till now and then increments the value of i by 1
our expresion becoms
i = 0 +... and i becomes 1
so now the value of i after + sign is assigned 1( as now i = 1)
so the final expression becomes ( till now i is one)
i = 0 + 1
now calculation the expression we get
i = 1
if u got this now we will go back to ur question
now our expression is
i = i++
first i=0 so after the = sign i is assigned 0 and the value of i is incremented by 1 and i becomes 1
and our expression becomes
i = 0 ( till now the value of i is one)
but after evaluating this expression i becomes 0
Sudhir Thorat

Joined: Dec 27, 2000
Posts: 5
Does this mean that the following statements
public class Postincrement{
public static void main(String args[]){
i=i++ + i++;
System.out.println(i) ;
would print 0 but the value of the variable i is 2 ?
anjan bhushan
Ranch Hand

Joined: Dec 12, 2000
Posts: 71
I think confusion is due to behaviour of operator in C.
In C it will do
i= 0;
i=i++;/* 2 */
ans i =1
C do it as assign i=0 on line two then increment it by 1.
Java seems workingin different way

I am the most eligible bachelor in whole world, but only known in limited territory!!!
Digital Intoxication Blog
I agree. Here's the link:
subject: how the assignment happened
Similar Threads
Resetting the array size
Why does this give Null Pointer Exception
what is the Output? and why?