• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why there is no compiler error ?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When I tried to call method as it printed "Var args method" as output . But why didn't it give compiler error as in printUs method takes 2 arguments and i am calling method with just 1 argument.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A Agr wrote:

When I tried to call method as it printed "Var args method" as output . But why didn't it give compiler error as in printUs method takes 2 arguments and i am calling method with just 1 argument.


It is because you can collect zero elements into the var-args array. In your method, print args.length and see what you get (when you call with only one argument that should be 0.)

That also means that if your method were printUS(String... args) you would be able to call it without any arguments.
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is new feature from Java 5.0 called var args... You can define method so that you can pass different number of arguments. But, var args always will be the last argument in a method.
 
A Agr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ruben,I got it.

I have a quick question here that will the following 2 method calls be same ?



and

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A Agr wrote:Thanks Ruben,I got it.

I have a quick question here that will the following 2 method calls be same ?



and


Hi,

They won't be the same. If you print the length of the vararg array, in the first case it will be 0, but in the second case it will be 1 (since it will contain an empty String.)
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
:deleted:
 
A Agr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thanks
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is easy to understand and remember as well , if you know the following fact
if declare a method like
private static void printUs(String abc, String... args)
then comipler would tranfor it to
private static void printUs(String abc, String[] args)

and your calls to this method would be tranformed as bellow
printUs("hi")
tranformed to
printUs("hi",new String[0]) // comipler would place a String array of length 0


printUs("hi","user!","how","are","you?")
tranformed to
printUs("hi",new String[]{"user!","how","are","you?"}) // comipler would place a inline String array
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic