Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does this method call compile with only one argument ?

 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because String... is a var-arg which is a convenience for passing an array.

So if you do



The compiler will turn it into



which is a valid String array.

If you omit this argument completely as in your example, the compiler does this:



Which is a 0 sized String array and perfectly legal.
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Harry,

... operator represents 0 or more arguments . In this case the function can be called in various formats.. I believe, the below code would clear your doubt.




The output would be

The array elements are -
21 22
The array elements are -
20 30 40
The array elements are -
1 2 3 4
The array elements are -

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic