Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

increment!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Twister {
public static void main (String[] arguments) {
int i = 1;
i = i++;
System.out.print(i);
}
}
The answer is given as 1.I thought that the answer is 2.
As per the code, my Understanding is that i assigns 1 to i and the i gets increments.Since the output is the same i, the result should be 2 rite?Can anybody explain pl!Thanks
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sridhar,
The i++ is a Postfix increment operator. It has the following semantics according to Mughal:

Hope this helps!
[ February 12, 2004: Message edited by: Owee Nicolas ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sridhar,
I agree with Owee.
I don't know about the internal workings precisely, but as far I figured what happens is that the value of the primitive variable is actually modified when either increment and decrement operator is applied (postfix or prefix i.e., i++ or ++i). But in the case of post increment & decrement operators the value (after modification) -1 and +1 respectively is returned.
Thus when this value is assigned back to itself the modified value is overridden. When you go to the next line the value prior to the operator being applied is maintained.
Well this is as per my analysis. Hope the more knowledgeable guru's can elaborate.
related link
related link2
Regards,
Isuru (SCJP 1.4)
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I understand the prefixoperator.As per my understanding, in my question if I say int j=i++, the values of i is first assigned to j and then i gets incremented.
But in my question it is i=i++ and so value of i is incremented rite?can anybody explain me please?Thanks
 
Owee Nicolas
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sridhar,
I would like to apologize for my last message. I think while editing my reply I deleted the semantics definition. According to Mughal:


Postfix increment operator (i++) uses the current value of i as the value of the expression first, then adds 1 to i.


So when you assigned variable i=i++, you assigned the current value of i which was 1 and not incremented value. But if you want to get the incremented value of i then do the Prefix increment operator i=++i.
Hope this helps!
 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Owee,

Doubt on sridhar's question:
int i = 1;
i = i++; //Line 1
How can i get the incremented value of i after the execution of Line 1.

i.e. if i say,

int i = 1;
i= i++;
int b = i; //Line 2
System.out.println(b);

will I get the value of b as 2( since we r using a post increment operator)? pls explain in detail.
vineela
 
Owee Nicolas
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vineela,


int i = 1;
i= i++;
int b = i; //Line 2
System.out.println(b);
will I get the value of b as 2( since we r using a post increment operator)?


No, you will get the value of 1. Let me explain. First, the variable i was originally assigned the value 1:
int i = 1; <--- value i is 1
When you assigned the variable i to a post incremented variable i. It got it's current value of 1:
i= i++; <--- value i is 1
However, the variable i was not incremented since i was already assigned the value 1. If you had assigned a different variable like j:
j= i++; <--- value j is 1
The value of variable i would be increment. So on the next assignment:.
j= i; <--- value of j is now 2
Hope that helps!
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry owe but not clear with the answer. Can anyone please explain the answer correctly.
thanks
shekar.
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
People,
Think about it this way:

int i = 1; //i = 1
i = i++; i is assigned the value of what i is (which is 1) increment later
System.out.print(i); //i is still 1 the increment did not happen

another way i found helped me understand it is:
int i = 0;
i = i++ + i++ + ++i + ++i; //we will break down
i++ //equals 0 then increment
+
i++ //equals 1, then increment
+
++i //the last was post-increment so value was 2 but this is pre-increment value of 2+1 is 3
+
++i //preincrement 3+1 is 4
so i= 0+1+3+4
1 = 8
adding another post increment will show exactly that the last post-increment does not get included.
i = i++ + i++ + ++i + ++i + i++;
same as above until 8 but then add the vlaue of i which is 4 to the variable i but it does not increment.
i = 0+1+3+4+4
i = 12
Hope this helps with the increments.
Davy
P.s. with the above try to find the value of without compiling
int k = 0;
k = k++ + ++k + ++k + k++ + ++k + k++ + k++ + k++ + ++k + k;
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Way to go Davy, where did you get that example.
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
off of your kind self,
well vicken, did you think i helped in a positive manner??
Davy
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure you did... your example is crystal clear, I am pretty sure the guys upstairs understood it.
Good Job.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although the problem has been explained quite beautifully.
But the concept simply is,

The ASSIGNMENT operator has precedence over the POSTFIX increment operator

Correct me if I am wrong.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aabdullasm,
I think this is the distilled reason of why i=i++ leaves i an unaltered state. Personally I find this way of looking at 'why' much more instructive.
Actually though, an example like i = i++ + ++i + i++ etc is also helpful because it sort of motivates an understanding through a more involved example.
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abdullasm,
yes I can see where you are going:
int i=0;
i = ++i; // equals 1 i is equal to pre-incremented i
but i = i++; //equals 0 i is equal to i (then post-increment) does not get seen, so it is really i = i
Hope this helps
Davy
p.s. test it out
[ February 12, 2004: Message edited by: Davy Kelly ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by abdullasm mamuwala:
Although the problem has been explained quite beautifully.
But the concept simply is,

The ASSIGNMENT operator has precedence over the POSTFIX increment operator

Correct me if I am wrong.


On the contrary abdullasm, the postfix and the prefix operators has higher precedence over the assignment operator, they are always executed first.
However the key part in this question is that the
  • postfix operator: returns the old value then increment it.
  • prefix operator: increment the value then return it.

  •  
    She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
    Free, earth friendly heat - from the CodeRanch trailboss
    https://www.kickstarter.com/projects/paulwheaton/free-heat
    reply
      Bookmark Topic Watch Topic
    • New Topic