Order of execution of parameters guarantees in Java?
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.
tpdi
Yes, I'm looking for a job. Let me know if you have openings to fill. Answers I'm particularly proud of: c-using-namespaces-to-avoid-long-paths sql-query-to-collapse-duplicate-values-by-date-range a-better-way-to-replace-many-strings Answers that are edited five or more times no longer gain reputation. This discourages editing, making answers less good. Please click here to read my full post about why the "Community Wiki" penalty is bad for the site and its members. I have a gmail account. It is tomdatafellow@.
Updated on June 14, 2022Comments
-
tpdi almost 2 years
Given the following function call in
C
:fooFunc( barFunc(), bazFunc() );
The order of execution of
barFunc
andBazFunc
is not specified, sobarFunc()
may be called beforebazFunc()
orbazFunc()
beforebarFunc()
inC
.Does
Java
specify an order of execution of function argument expressions or likeC
is that unspecified? -
Jon about 14 yearsWhile 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.
-
trashgod about 14 yearsIndeed, "It is recommended that code not rely crucially on this specification." java.sun.com/docs/books/jls/third_edition/html/…
-
John Henckel over 9 years@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.
-
CoffeDeveloper about 9 yearsI 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.
-
Steven Jeffries about 8 yearsJoining 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.
-
Jeff G about 8 years@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.
-
raven about 5 years@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.