File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes [n] Strange Variable Argument behaviour? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "[n] Strange Variable Argument behaviour?" Watch "[n] Strange Variable Argument behaviour?" New topic

[n] Strange Variable Argument behaviour?

Nirav Patel

Joined: May 27, 2005
Posts: 22
Predict the output of following code.
pass 1 2 as command line arguments.

Shouldn't it be "Two arg printUS" followed by the two values?
It's not.
Answer is "in printUS" followed by the two values.
If i call explicitely than it calls specific method for two arg paramers but not when it's from command line.
After all here command line is also an array of

Any idea?

{JDK 5.0, Windows XP Pro}


----<br />Nirav Patel<br />Software is the bottleneck in the digital world!
Nirav Patel

Joined: May 27, 2005
Posts: 22
Hi group!
Is there any problem with my question or am I in the wrong forum?
Tapas Kumar

Joined: Jul 29, 2005
Posts: 6
Hi Nirav,
Acccording to my understanding vararg String ...b is equivalent to
String[] b. Before java 1.5 a developer has to explicitly move string values to an array and call the method. Java 1.5 does this for us behind the scene.
Your method call is therefore with an array reference not with two String type parameter.Result should be as you got.
Hope this helps
I agree. Here's the link:
subject: [n] Strange Variable Argument behaviour?
It's not a secret anymore!