• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

dout abt postfix operator

 
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

Now according to the postfix rule i is incremented by 1 after the old value had been assigned to i

So the fact that i is incremented should give us 12 in the output

Please tell me where i am going wrong

Thanks
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats true..but
line 2>>>>>>>> you have assigned 11 to i
and then in line 3..you have 11 value assigned to i again and later incremented...but this incremented value has not been assigned to i (i still has 11 value) that is the reason it prints 11..
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"..but this incremented value has not been assigned to i (i still has 11 value) that is the reason it prints 11.. "

Just try commenting i=i++; and include jus i++; the output is 12

So this implies that even though i has been assigned 11 and after that i is incremented "i" should be holding 12.

Therefore the output 12

Just saying that not happy with the explanation .... i think the logoc is something else

Thanks
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sid,

i is incremented during the evaluation of the right side. After the evaluation (and thus after incrementing i) the value is assigned to i.
 
suresh koutam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
agreed with your answer...but you are reassigning the value of 11 to i...
there is difference when you are using
i =i++ ( you have assigned the value 11 here to i and then you are incrementing it but not assigning it back to the variable)
and just i++ (you have incremented the variable and it is retained...what ever changes you make to the variables on RHS side will not be retained except for the increment operators)

do you think this is tru...
int a =5;
int b =10;
int c = a + 5;

now, has the value of "a" changed since you have added 5 to it...??? no
similarily is the case with the assignmnet operators in you example..


Suresh
[ July 09, 2007: Message edited by: suresh koutam ]
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
super cool really appreciate it !!! thanks alot !!!
 
His brain is the size of a cherry pit! About the size of this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic