Methods with variable argument list
NickName:developer Ask DateTime:2011-05-19T01:01:32

Methods with variable argument list

I am preparing for SCJP and I came to know Methods with variable argument list. I have couple questions.

  1. What is "Methods with variable argument list".
  2. When to use "Methods with variable argument list".

Copyright Notice:Content Author:「developer」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6048345/methods-with-variable-argument-list

Answers
hvgotcodes 2011-05-18T17:02:36

Its a language feature that allows you to declare a method that will take any number of arguments.\n\nAccordingly, you use it when you don't/can't know how many arguments you will pass to the method. Look at the String.format method. In the method declaration, the final parameter is Object... args which indicates that format can take any number of arguments.\n\nSee also this: http://download.oracle.com/javase/1,5.0/docs/guide/language/varargs.html",


kuriouscoder 2011-05-18T17:06:52

Methods with variable arguments is achieved by triple dot operator ... . As name suggests, it is used when you have variable argument list. Functionality wise it's similar to pass a single dimensional array of the arguments with one exception that atleast one argument needs to be supplied. Otherwise, it's sometimes preferred over single dimensional array as matter of style. If you look at the caller's code, you would get an idea of how many arguments are passed explicitly. However, if you have more than manageable number of inputs, passing it as array or collection would make more sense.",


anubhava 2011-05-18T17:06:31

A very simple and practice example of method with variable arguments is String#format method.",


More about “Methods with variable argument list” related questions

Methods with variable argument list

I am preparing for SCJP and I came to know Methods with variable argument list. I have couple questions. What is "Methods with variable argument list". When to use "Methods with variable argument ...

Show Detail

Variable length argument list with default argument?

Is it possible to set a default argument for a variable length argument list ? Example: def foo(args: String*) = args.foreach(println) How to set a default argument for args ?

Show Detail

Augmenting variable argument list

Is there a way for a function to append to variable number of arguments it already received? I'm using __builtin_va* of libgcc for ARM to implement variable argument functions (typedef'ed appropria...

Show Detail

How to pass variable argument list in PHP

I have a function that is always called like this: old_function(sprintf("Some arbitrary data %d, %s, %f\n", $i, $s, $f)); Is it possible in PHP to make a function that combines these to functions...

Show Detail

default values for variable argument list in Python

Is it possible to set a default value for a variable argument list in Python 3? Something like: def do_it(*args=(2, 5, 21)): pass I wonder that a variable argument list is of type tuple but...

Show Detail

Making Variable Argument List function

I have made one simple variable argument list function in C. But it does not work. When I call that function with one argument, and then check that argument inside that function, that argument lost...

Show Detail

Concatenate variable argument list

I want to prepend a String to a variable list of String arguments. public String myMethod(String... args) { return myOtherMethod("some string" + args); } Now, this of course will not work, be...

Show Detail

Call 2 methods through an argument in method in java

I have 2 methods in interface: public interface ISort { public void insertionSort(final String[] a); public void selectionSort(final String[] a); } Let's call method, which receives metho...

Show Detail

How to put a part of variable argument list into another function?

I'm dealing with such a problem, I have function f(std::initializer_list<double> list),and I want to put a part of variable argument list (the second variable argument to end) into another fu...

Show Detail

Retrieve the length of variable argument list

function countargs(...) return #arg end > countargs(1, 2, 3) 0 In this case, countargs returns 0 and not 3. How to retrieve the length of the variable argument list? I tested against Lua 5.3...

Show Detail