views:

148

answers:

1

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().

Does Java specify an order of execution of function argument expressions, or like C is that unspecified? If so, can you quote chapter and verse?

Thanks.

+10  A: 

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.

Michael Easter
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
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