java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.lang arrow How to pass unspecified number of arguments to a method
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3947
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to pass unspecified number of arguments to a method E-mail
User Rating: / 59
PoorBest 

JDK 5.0 provides a feature to pass unspecified number of argument to a method. Java treats the variable-length argument list as an array. This is represented by argument type followed by three dots in the declaration of the Method.

The method int sum(int…numbers) can take any number of parameters of type integer.

public int sum(int...numbers) {

        int sum=0;
        for (int d : numbers )         
            sum =sum + d;
        return sum;
    }

    public static void main(String[] args) {
        int i=3;
        int j=6;
        int k=9;
        System.out.println("Sum  of two numbers"+sum(i,j));
        System.out.println("Sum of three number"+sum(i,j,k));
    }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.