skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Friday, October 5, 2012

Java - Unknown Number of Parameters in Methods for JDK1.5

Posted by Raju Gupta at 11:40 PM – 0 comments
 
The JVM identifies methods that can take different number of parameters using the three dots "..." in the method declaration and handles different number of arguments correspondingly. This asset makes use of the JDK1.5 Enhanced for loop as well. Pls check my other mighty asset on using the enhanced for loop.

package test;

public class UnknownNumOfParamsForMethodTest {

 public static 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));
    }

}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger