76

Given the following function call in C:

fooFunc( barFunc(), bazFunc() );

The order of execution of barFunc and BazFunc is not specified, so barFunc() may be called before bazFunc() or bazFunc() before barFunc() in C.

Does Java specify an order of execution of function argument expressions or like C is that unspecified?

Hash
  • 4,647
  • 5
  • 21
  • 39
tpdi
  • 34,554
  • 11
  • 80
  • 120
  • related question: [Order of execution of methods describing an instance and an argument in Java?](http://stackoverflow.com/questions/13724952/order-of-execution-of-methods-describing-an-instance-and-an-argument-in-java/13725048) – bacar Dec 05 '12 at 14:26

1 Answers1

86

From the Java Language Specification (on Expressions):

15.7.4 Argument Lists are Evaluated Left-to-Right

In a method or constructor invocation or class instance creation expression, argument expressions may appear within the parentheses, separated by commas. Each argument expression appears to be fully evaluated before any part of any argument expression to its right.

Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
Michael Easter
  • 23,733
  • 7
  • 76
  • 107
  • 9
    While this is true, please please don't code in a way that makes it dependent on execution order. It's just adding complexity without adding functionality. – Jon Feb 04 '10 at 17:50
  • 8
    Indeed, "It is recommended that code not rely crucially on this specification." http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7 – trashgod Feb 04 '10 at 22:13
  • 51
    @Jon I disagree! if it is in the specification, then you can rely on it. For example, to read a rectangle from a file, I use this code: myRect = new Rectangle(scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt()); It is concise and simple. A longer implementation would be unnecessary complexity. – John Henckel Oct 31 '14 at 16:44
  • 5
    I used that to avoid saving local variables in many places and made code shorter 1/2 lines for each funcion call in a parser :D. Avoiding many local variables (unless that creates more complex code) is usually good design too. – CoffeDeveloper Feb 01 '15 at 17:08
  • 4
    Joining this party a bit late, but I have a method to combine bytes to an int that I call like this: toInt(data[offset++], data[offset++], data[offset++], data[offset++]). I'm far too lazy to change this code, so I'm glad it will work. – Steven Jeffries Feb 10 '16 at 01:20
  • 7
    @Jon If Java allowed temporary variable declaration prior to constructor chaining calls, I would agree with you. However, I see no viable alternative to relying on this behavior when combining constructor chaining with complex member initialization. – Jeff G Mar 17 '16 at 23:32
  • @JohnHenckel Your example is fine. But imagine if instead of Rectangle you had new MyClass(...) and after a while you decide to reorder MyClass's constructor arguments; the IDE will automatically fix the usages for you but this results in a hard to find bug since the order of the argument resolution should have been preserved. You should be careful when using this. – raven Apr 06 '19 at 08:51