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 / July 2007

Tip: Looking for answers? Try searching our database.

what is "..." in Java Language?

Thread view: 
Neo - 25 Jul 2007 05:55 GMT
Hi,

I was studying Google Open Source Project, name "Google Singleton
Detector". This project detect Singleton classes from given classes.
when I started study this project. I found one thing in code which is
not clear for me is "..." in function parameters. Code snap is below,

 public SingletonDetector(ClasspathRoot classpath, String prefix,
     String... classNames) {
   this(classpath, prefix, new Flags(), classNames);
 }

File Name: SingletonDetector.java Project URL:
http://code.google.com/p/google-singleton-detector/

Please guide me, what is "..." in Java language? When it is introduce
in Java?

Regards,
-aimslife
Joe Attardi - 25 Jul 2007 06:19 GMT
> Please guide me, what is "..." in Java language? When it is introduce
> in Java?

"..." means that the method takes a variable number of arguments. You
can pass zero, one, or many arugments in place of the "...", then
iterate over the items passed in. It was a new feature of Java 5.
Roedy Green - 25 Jul 2007 07:09 GMT
>"..." means that the method takes a variable number of arguments. You
>can pass zero, one, or many arguments in place of the "...", then
>iterate over the items passed in. It was a new feature of Java 5.

instead of

void myMethod ( String [] things )

you can say:

void myMethod (String... things )

Inside the method you treat them identically.

Outside you can say  myMethod ( new String["birch", "oak"] )
or myMethod ( "birch","oak")
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Mohammad Omer Nasir - 25 Jul 2007 07:58 GMT
Hi Roedy,

I wrote function its prototype is "void nParameters(int... arguments)"
and when i tried below code then it gave me exception.

DriverControl driver = new DriverControl();
driver.nParameters(new int[1, 2, 3, 4, 5]);

Exception Statement:
Exception in thread "main" java.lang.Error: Unresolved compilation
problems:
    The method nParameters(int...) in the type DriverControl is not
applicable for the arguments (int[], int, int, int, int)
    Syntax error, insert "]" to complete Dimensions
    Syntax error on token "]", delete this token

Its mean we can not pass array to N-parameter function pram.

Please refer me, some links to study N-Parameters in detail.

Regards,
-aimslife
Kevin Bagust - 25 Jul 2007 09:12 GMT
> I wrote function its prototype is "void nParameters(int... arguments)"
> and when i tried below code then it gave me exception.
>
> DriverControl driver = new DriverControl();
> driver.nParameters(new int[1, 2, 3, 4, 5]);

This line is wrong. It should be:
driver.nParameters(new int[]{ 1, 2, 3, 4, 5});

> Exception Statement:
> Exception in thread "main" java.lang.Error: Unresolved compilation
[quoted text clipped - 5 lines]
>
> Its mean we can not pass array to N-parameter function pram.

No. If you read the messages the compiler has decided to alter the
incorrect line (for some reason) to:
driver.nParameters(new int[1], 2, 3, 4, 5);

Which the compiler then complains that it cannot find the method.

Kevin Bagust.
AimsLife - 25 Jul 2007 09:37 GMT
Thanks! All!

Regards,
-aimslife
AimsLife - 25 Jul 2007 09:36 GMT
Hi Roedy,

> DriverControl driver = new DriverControl();
> driver.nParameters(new int[1, 2, 3, 4, 5]);
[quoted text clipped - 6 lines]
>         Syntax error, insert "]" to complete Dimensions
>         Syntax error on token "]", delete this token

I done it! I was passing array as a parameter in wrong way thats why
exception comes.

Code Correction:
        DriverControl drive = new DriverControl();
        drive.nParameters(new int[]{2, 5, 6, 8, 45});

we can pass array to N-Parameter argument. DriveControl is class has
main function.

Regards,
-aimslife


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



©2008 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.