• Post Reply Bookmark Topic Watch Topic
  • New Topic

Var-Args question  RSS feed

 
Russ Russell
Ranch Hand
Posts: 72
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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...
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I did not think about this scenario. Thanks!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!