Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Variable Arguments

java variable type

  • Please log in to reply
7 replies to this topic

#1 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 05 October 2009 - 02:17 PM

Java Variable Arguments

Java allows you to pass a variable number of arguments to a function. We specify several arguments with a variable type followed by an ellipise and a name. The variable number of arguments must come as the last parameter of the function.

Example:

public static int sum(int... nums) {
        int nSum = 0;

        for (int x=0;x<nums.length;x++) {
            nSum += nums[x];
        }
        return nSum;
    }

This function takes a variable number of arguments and returns the sum of all the arguments. The key here is that you treat the variable arguments as an array.

Use this code to test out the function:

System.out.println(sum(3,4,2,5));
System.out.println(sum(-1,2));
System.out.println(sum(9,3,5,3,2,3,4,3,2,3,2,1,9,-5));


There is also a foreach loop that you can use in the function.

public static int sum(int... nums) {
        int nSum = 0;

        for (int i: nums) {
            nSum += i;
        }
        return nSum;
    }

This code basically does the same thing as above. It assigns each item in the array of parameters to i and then increases the sum by i. They both do the same thing. However, I like the first loop better because it allows me to control where in the array I start and where in the array I stop.


Combining varargs with static arguments


You can use a variable list and static number of arguments. The key here is that the variable arguments list must be declared at the end of the parameter list.

E.g.

 public static int product(int n, int... nums) {
        int product = 1;

        for (int i=0;i<nums.length;i++) {
            product *= (nums[i]);
        }
        product *= n;
        return product;
    }

In this code the user must pass one integer and then as many integers as they want.

Example:

System.out.println(product(3, 4,2,5));

In this case, 3 is the integer that must be passed. The rest of the integers are optional.

E.g.

 System.out.println(product(3));

Hope this helps! Also it is nice to note that the same functionality exists in C and C++.
  • 3

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 06 October 2009 - 08:08 AM

I didn't know Java had this as well. +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 07 October 2009 - 06:13 AM

Me neither. That's great! +rep.
  • 0

#4 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 07 October 2009 - 06:16 AM

Glad to see you wrote it! Nice work. +rep
  • 0

#5 ThemePark

ThemePark

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 147 posts

Posted 07 October 2009 - 12:42 PM

+1 of not knowing this also existed in Java. So +rep for teaching us that.
  • 0

#6 debtboy

debtboy

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 499 posts

Posted 07 October 2009 - 02:35 PM

Just now noticed this one
+rep :)
  • 0

#7 Phineas

Phineas

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 13 January 2010 - 03:32 PM

Nice work, would rep++ if I knew how.
  • 0

#8 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 13 January 2010 - 05:51 PM

Click the justice-scale icon next to #1 (permalink) in his post header.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download