开发者

Execution priority of expressions

In the following line of code:

x = x.times(x).plus(y);

in what order are these expressions going to be executed?

Will it be like:

x = (x + y)*x

or x = (x^2) + y, or something else and why?

Links to documentation about the specifi开发者_如何学编程c subject will be highly appreciated as I had no luck with my search. Apparently I don't know where to look at and what to look for.

Thank you.


These are methods; the fact that they are called "plus" and "times" doesn't mean that they'll necessarily follow the behaviour of the built-in + and * operators.

So x.times(x) will be executed first. This will return a reference to an object, on which plus(y) will then be executed. The return value of this will then be assigned to x. It's equivalent to:

tmp = x.times(x);
x = tmp.plus(y);


Here's a link to a documentation which most likely contains the required answer (probably at 15.7). It's highly technical and verbose but not inaccessible to most people (I believe).

However, it seems that you're just starting programming, so you'll be better off reading other answers here, and programming more to get an intuitive feel (not exactly a 'feel', as it's systematic and rigourous) of the order of operations etc...

Don't be afraid to write "throw-away" code (which you can incidentally save too) to find out things you don't know if you don't know where else to look for the answer. You can always google more intensively or dive through the language specs at a latter date. You'll learn faster this way. :)

One simple way to find out is to write something like this:

class Number{
  private int number;

  public Number(int x){
    number = x;
  }

  public Number times(Number x){
    System.Out.PrintLn("times");
    return number * x;
  }

  public Number plus(Number x){
    System.Out.PrintLn("plus");
    return number + x;
  }
}


Method chains get executed from left to right, with each method using the result from the previous method, so it will be x = (x^2) + y.


What you're referring to in the algebraic expressions is operator precedence - evaluating multiplications before addition, for example. The Java compiler knows about these rules for expressions, and will generate code to evaluate them as you expect.

For method calling, there are no "special rules". When given x = x.times(x).plus(y); the compiler only knows that to evaluate x.times(x).plus(y), it first needs to know what x is, so it can call times on it. Likewise, it then needs to know what x.times(x) is so it can call the plus method on that result. Hence, this type of statement is parsed left to right : (x * x) + y.

Some languages allow the creation of functions that are "infix" with user supplied precedence. (such as Haskell : See http://www.haskell.org/tutorial/functions.html, section "Fixity declarations"). Java is, alas, not one of them.


It's going to be executed in left-to-right order, as

x = (x.times(x)).plus(y)

The other way:

x = x.(times(x).plus(y))

doesn't even make sense to me. You would have to rewrite it as

x = x.times(x.plus(y))

to make sense of it, but the fact that the second x is contained within times() while the y is outside it rules out that interpretation.


The reason the documentation doesn't say anything about this is probably that such expressions follow the normal rules for how a statement like a.b().c().d() is evaluated: from left to right. We start with a and call the function b() on it. Then, we call c() on the result of that call, and we call d() on the result of c(). Hence, x.times(x).plus(y) will first perform the multiplication, then the addition.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜