7

I'll call a method with two arguments, but i'll use k++ like this:

polygon.addPoint((int)rs.getDouble( k++),(int)rs.getDouble( k++ ));

Actually i want to be sure that jvm executes first argument first, then the second one. If somehow the order will change, arguments would be passed wrong order.

Thanks a lot!

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Ismail Yavuz
  • 6,727
  • 6
  • 29
  • 50
  • Creating two distinct integers would make this code easier to maintain IMHO. – sp00m Mar 27 '14 at 15:57
  • Although the ordering should be guaranteed, I'd not rely on this, especially if there are simple ways to get rid of that dependency. – Thomas Mar 27 '14 at 15:58

3 Answers3

10

Yes, the arguments are guaranteed to be evaluated left-to-right. Any compiler complying to JLS rules should follow that. This is mentioned in JLS §15.7.4:

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
9

Yes, Java guarantees that. However, this does not mean that using the code like that is a good idea: readers of your code may get thoroughly confused.

It is much cleaner to show the order explicitly:

int a = (int)rs.getDouble(k++);
int b = (int)rs.getDouble(k++);
polygon.addPoint(a, b);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

Java does not support named parameters, so you are correct in understanding that parameters are read in position.

For example, a method with the signature (int, String) and a method with the signature (String, int) will never be confused with each other, Java takes the type and sequence of parameters into consideration when figuring out what method to call.

Likewise, in your method, you will always have parameters coming in in a predictable and uniform fashion.

For that reason, the first k++ will always execute first, and the second k++ always after that, and that is guaranteed behaviour.

Ewald
  • 5,691
  • 2
  • 27
  • 31