• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

incrementation of variable

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends;
i have a doubt abt incrementation of variable.
see the following code:


In the above code the o/p is 1.
But by logical view the o/p should be 2 as shown in the commented code.
Why x is not getting incremented after line 3?

Bye......
shubha.

( tags added)
[ June 16, 2005: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Because x = x++; is still the same.

Your saying x = x++; it's not being incremented x is just = x.

In x++ the ++ (postfix is being ignored) you need to use ++x; if you want to increment it.

Hope this made sense!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may find that Corey's tip help to explain what's happening.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your commented code is different than the code you run - they are not equivilent.

here's why...

JUST before we get to line three, x is 1. we now start doing what line 3 says to do...

first, get x. that's 1. we'll remember that value for later, first i have to take care of that ++. So, now i increment x. x is 1, so i'll increment it to 2. now, lets take care of that assignment thing. the right hand side of my statement was... let's see, i remembered that... oh yeah!!! it's 1!!!.

so now, i'll assign 1 to x.

and so it prints 1.
 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone;

i have still doubt,

if we run the code as;

class Test{
public static void main(String args[]) {
int y=0;
++y;
y++;
System.out.println(y);
}
}

gives me o/p as 2.In the 1st code I did the same operation.
 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone;

i have still doubt,

if we run the code as;

class Test{
public static void main(String args[]) {
int y=0;
++y;
y++;
System.out.println(y);
}
}

gives me o/p as 2.In the 1st code I did the same operation.
 
bronco
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x++;

and

x = x++;

Do NOT do the same thing.

The first one says "increment x."

The second one is a bit confusing. It says...

- grab the current value of x (let's assume it's 0)
- increment x (it's 1)
- assign the grabbed value to the variable on the left (x)
- so x gets 0 (the value we grabbed)

If you changed:

x = x++;

to:

x = ++x;

You'd get something more like what you expect because x gets incremented BEFORE the assignment takes place.

Of course, no one in their right mind would actually write either of these since just writing:

x++;

is MUCH more clear.

Hope that helps.
[ June 16, 2005: Message edited by: Dave Wood ]
 
You can't have everything. Where would you put it?
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic