Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • 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

incrementor question

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Static
{
static
{
int x = 5;
}

static int x,y;
public static void main(String args[])
{
x--; myMethod();
System.out.println(x + y + ++x);
}

public static void myMethod()
{
y = x++ + ++x;
}
}


My question is why does "y" in myMethod evaluate to 0.

Happy Studying
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets Evaluate

static
{
int x = 5;
} this is block variable so wont matter.

static int x,y; here x = 0, y=0
x-- x is still 0 post operator --- @1
myMethod()
y = x++ + ++x;

here x++( would make x = -1 as the post o/p @1 would get result)
++x ( this would cause the previous x++ to give value (-1 + 1) 0 and then
-1 + 1 + 1= 1)

so y = -1 + 1 = 0

Hope this helps
 
Annette Sovereign
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here x++( would make x = -1 as the post o/p @1 would get result)

Hi there,

Thanks for responding.

I'm sorry I'm not sure I understand what the statement means. How x++ would make x become equal to -1 is a mystery to me. I was able to evaluate the expression to 0, but I'm not sure my reasoning is correct.

If someone can break it down, that would be great.

I'm trying to work out when the post increment operator on x is applied.


Happy Studying
 
Author
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When myMethod is entered, the value of x is -1.

The order of evaluation on the y = x++ + ++x statement is as follows:

x++ - Pre-evaluation, x=-1. This expression will evaluate to -1, then set x=0.
++x - Pre-evaluation, x=0. x will be set to 1 with the pre-increment operator. The expression will then evaluate to 1.

So we have: y = -1 + 1, and thus y = 0. Hope that makes sense?
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic