• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how the assignment happened

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
rajani
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
System.out.println(i);
}
}
But the answer is 1 !!. Really it is a strange assignment.
I tried the same in C/C++ it's giving 1.
Regards,
vadiraj

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
HTH
anil
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this mean that the following statements
public class Postincrement{
public static void main(String args[]){
i=0;
i=i++ + i++;
System.out.println(i) ;
}
}
would print 0 but the value of the variable i is 2 ?
Thanks
Sudhir
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think confusion is due to behaviour of operator in C.
In C it will do
i= 0;
i=i++;/* 2 */
printf("%d",i);
ans i =1
C do it as assign i=0 on line two then increment it by 1.
Java seems workingin different way
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic