1

Are there some benefits of using variable-length arguments instead of just passing an array to method in Java. Thank you.

  • It depends on what you are trying to do. Add the relevant code and the context. – Guy Jul 06 '17 at 11:16
  • Ok. I would say, in which case one is better than the other? – ilya.gromov Jul 06 '17 at 11:17
  • It's just syntactic sugar; but remember that you can also pass an array to a method declared with a variable-length argument. – Maurice Perry Jul 06 '17 at 11:25
  • The main benefit is for readability purposes. Although things become less readable as you add more parameters! It is down to personal choice, but I would say a method accepting 10 parameters is certainly not benefiting from ease of readability. I would even go as far as to say 5 parameters is becoming hard to read. At this point, and probably before, you want to convert those parameters into a single object (probably not an array though). – khriskooper Jul 06 '17 at 11:26
  • Also, it is a question of extensibility / flexibility, as you can easily add parameters as fields to a single object, rather than having to refactor all the calls to the method. So if your code is in constant flux, this may be the better way to go after all. – khriskooper Jul 06 '17 at 11:26
  • (I'd written all that already but then someone closed the question to new answers so I though I would post it here anyway :-) – khriskooper Jul 06 '17 at 11:26

0 Answers0