Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

a newbie's question varargs(....)

Thread view: 
Mike - 17 Apr 2007 07:48 GMT
Hi

I've read java doc.
I cannot understand what varargs(....) is .
Can someone give me a very very simple example?

thanks.

Mike
Daniel Pitts - 17 Apr 2007 08:00 GMT
> Hi
>
[quoted text clipped - 5 lines]
>
> Mike

class VeryVerySimpleExample {
   public static void printArgs(String...args) {
       String[] array = args;
       System.out.println(array.length + " " + args.length);
       for (int i = 0; i < args.length; ++i) {
           System.out.println(args[i]);
      }
   }

  public static void main(String[]args) {
     printArgs();
     printArgs("Hello");
     printArgs("Hello", "World");
     printArgs(args);
     printArgs(new String[] {"This", "is", "not", "a", "drill"});
  }
}

So, hopefully my example shows you that:
A vararg is simple syntactic sugar for an array.
The Java compiler will automatically create a new array if one isn't
passed in.
There are a few other details to know about, but thats the basis of
it.

Hope this helps,
Daniel.
Mike - 17 Apr 2007 08:58 GMT
> > Hi
>
[quoted text clipped - 34 lines]
> Hope this helps,
> Daniel.

thank you very  much.  Daniel.
Though it's a little difficult for me.

It's quite strange there's no space between String... and args.
  public static void printArgs(String...args) {
>         String[] array = args;
"array" is also an array with variable length.  right?

I tried to replace all the String to int, which is more familiar to
me.
I also do a little test.  I understand now.

thank you.

Mike
Jussi Piitulainen - 17 Apr 2007 09:07 GMT
...
> It's quite strange there's no space between String... and args.
>    public static void printArgs(String...args) {

No stranger than the space that is not between String[] and args:

  public static void printArgs(String[]args) {

You can put it in when you write your own code. I do:

  public static void printArgs(String [] args) {
  public static void printArgs(String ... args) {

Matter of taste, whether shared by many or few. Compiler cares not.
frustratedprogrammer@gmail.com - 18 Apr 2007 01:01 GMT
> You can put it in when you write your own code. I do:
>
>    public static void printArgs(String [] args) {
>    public static void printArgs(String ... args) {
>
> Matter of taste, whether shared by many or few. Compiler cares not.

Are you saying there is absolutely no difference between

public static void printArgs(String [] args) {
public static void printArgs(String ... args) {

If so why did they bother to introduce the new syntax for the second
in Java 5?
Daniel Pitts - 18 Apr 2007 02:15 GMT
On Apr 17, 5:01 pm, frustratedprogram...@gmail.com wrote:
> > You can put it in when you write your own code. I do:
>
[quoted text clipped - 10 lines]
> If so why did they bother to introduce the new syntax for the second
> in Java 5?

The only real difference is when passing values into it.

public void printArgs(String[]args); can only be passed an array
printArgs(new String[] {"a", "b", "c"});

But, void printArgs(String...args); can be passed an array, or several
values.
printArgs("a", "b", "c");

The feature allows one to make intent clear without excess syntax.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.