1

Let's say I call a method like this:

myBigMethod(
  getMethodParam1(arg1, arg2),
  getMethodParam2(arg2, arg3),
  getMethodParam3(arg3, arg4)
);

Will calls to getMethodParam1, getMethodParam2 and getMethodParam3 be called asynchronously by Java or not?

Robert Watts
  • 179
  • 2
  • 10
  • 2
    http://stackoverflow.com/questions/2201688/order-of-execution-of-parameters-guarantees-in-java - they are guaranteed to be called in order. – Mat Jan 12 '14 at 14:37

5 Answers5

4

Java will never do anything asynchronously if you don't tell it to do so.

And assuming that there is a method like myBigMethod(a, b, c) it is going to evaluate each parameter first before it can pass the result to the method. From left to right.

So your example is equivalent to:

a = getMethodParam1(arg1, arg2);
b = getMethodParam2(arg2, arg3);
c = getMethodParam2(arg3, arg4);
myBigMethod(a, b, c);
zapl
  • 63,179
  • 10
  • 123
  • 154
1

No, JVM will call getMethodParam's methods one by one. If you want call these methods in parallel you shoud do it by yourself

1

If you are implying something like Haskell's non-strict evaluation semantics, then rest assured that Java, just like any other major programming language, has strict semantics and arguments are passed by value only, which means that the values must be calculated first.

If your question is possibly about whether Java guarantees order of argument evaluation, then the answer is Yes, the expressions will always be evaluated from left to right.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Java will evaluate the arguments before the method is called. So in your case, getMethodParam1/2/3 will be evaluated before myBigMethod is called.

pveentjer
  • 10,545
  • 3
  • 23
  • 40
0

First of all you can simply check it with a test and a big for loop. Or even debug, you'll see that methods are called one by one. Think yourself, it could be very dangerous if JVM make such decisions by itself.

eugen-fried
  • 2,111
  • 3
  • 27
  • 48