• 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

Var-Args question

 
Ranch Hand
Posts: 72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I understand that a variable argument list in a method must be formatted like this: void hello(int... x){}

But isn't "String[] args" in the method: public static void main(String[] args) treated as a variable argument list?

Thanks,
-Russ
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just like any other method that takes an array or varargs, you can either declare

or


Inside the body of the method, it's the same either way: args is a String[].

For a caller of the method, in the first case, he has to pass a String[] when calling, but in the second case, he can call it like main("a", "b", "c"). However, in the case of main() this doesn't matter, because we almost never call main() directly from our code. The JVM calls it, and it handles it correctly regardless of which declaration you used.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not in the sense that you can call it. Try calling main("Hello", "World") in your application and see how far you get. Yes, the JVM will take a bunch of inputs and convert it into an array for you, but that's the JVM doing the work, not the compiler.

As it happens, though, you can declare your main method as main(String... args) if you want. It compiles to the same method, so it works as usual.

Edit: or what Jeff said
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replys. So are both ways considered valid var-args syntax? According to the SCJP Study Guide, this one is not considered valid (although it does compile and work):

And by the way, I can call my hellow world program from the command line using java Test "hello", "world". Or with any number of args inputed.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Russ Russell wrote:Thanks for the replys. So are both ways considered valid var-args syntax?



No. Varargs is only the (...) syntax. If a method is declared to take a Foo[], then you have to pass a Foo[]. But if it is declared to take Foo..., then you can pass (foo1, foo2, foo3). Inside the method, it's the same either way: a Foo[]. This is what I stated in my first reply. Not sure what part is not clear.

According to the SCJP Study Guide, this one is not considered valid (although it does compile and work):


It's valid in that it is a legal way to declare a method. It is not varargs though.


And by the way, I can call my hellow world program from the command line using java Test "hello", "world". Or with any number of args inputed.



That's a totally different issue than calling a Java method from within Java code. The command shell takes your "H" and "W" command line args, passes them at indices 1 and 2 in the argv parameter of a C function int main(int argc, char* argv[] ) (index 0 is the program being executed, e.g. java.exe) that is the entry point for the JVM, which in turn passes them appropriately to your main().

You cannot, however, in your Java code, pass "hello", "world" to a method that takes String[] rather than String...
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using String[] is not using varargs. You really have to use the ... for that.
What Jeff and Matthew said is that varargs are internally used as arrays. That means that anywhere that you have an array, you can turn that into varargs (provided it's the last argument of the method). The main method is no different. And because varargs are internally used as arrays, the method implementation doesn't need to change, and any existing calling code will still work.

The other way around will cause errors though. Not inside the method itself, but calling code will break.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a note on this - while the 2 methods may be similar on certain grounds, it should be noted that a var-arg will never be initialized with a null value, Even if the caller does not pass any argument. Whereas, if the method accepts an array, the caller does not have an option of not specifying the argument at all (at least an empty array or a null value has to be passed) and the method should be able to handle the null argument.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aditya Jha wrote:Just a note on this - while the 2 methods may be similar on certain grounds, it should be noted that a var-arg will never be initialized with a null value, Even if the caller does not pass any argument.


Never?
Doing so is an explicit attempt by the calling code to sabotage the method though. By omitting the arguments, the method will receive an empty array and not null; I think that's what you meant.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. I did not think about this scenario. Thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic