Arbitrary number of formal parameters

M

Martin

Hi there,

Is it possible in Java to write a method, which allows an arbitrary
number of formal parameters? I know it is possible in C# although I
forgot the syntax. So far, in Java I've only seen passing of a
Collection or Map of Arguments.
 
S

Stefan Ram

Martin said:
Is it possible in Java to write a method, which allows an
arbitrary number of formal parameters?

No. But it is possible to declare a method /that/¹ allows
an arbitrary number of /arguments/ at run-time. This only
needs a single formal parameter declaration
like »java.lang.Object ... arguments«. See:

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.1

»LastFormalParameter:
VariableModifiers Type ...opt VariableDeclaratorId
FormalParameter

[...] If the last formal parameter is a variable arity
parameter of type T, it is considered to define a formal
parameter of type T[]. The method is then a variable arity
method. Otherwise, it is a fixed arity method. Invocations
of a variable arity method may contain more actual
argument expressions than formal parameters. All the
actual argument expressions that do not correspond to the
formal parameters preceding the variable arity parameter
will be evaluated and the results stored into an array
that will be passed to the method invocation (§15.12.4.2).«

(", which" is used for a non-restrictive clause.)
 
A

Andreas Leitgeb

Eric Sosman said:
method(42, 42.0, "XLII", "zwei und vierzig");

Sidenote: in German language, numbers are written as
one word: "zweiundvierzig". If written as separate words,
then in some cases it's a sum rather than a number.
It *seems* a little bit like an arbitrary number of parameters,
but what's going on "beneath the covers" is that there's a
fixed parameter count, the final parameter is an array of
something, and the compiler automatically builds the array
at the point of the call.

There is some potential for confusion for unexperienced programmers.
Those, who are experienced, or know the JLS by heart, will of course
not have a problem to predict (except for the hashCodes) the results
of this example:

import java.util.Arrays;
class Test {
static void foo(Object ...oa) { System.out.println(Arrays.toString(oa)); }
static public void main(String[] args) {
Object oa[]=new Object[10], ob=new Object[10];
foo(oa,ob);
foo(oa);
foo(ob);
}
}
//For an extra, change "oa[]=" to "[]oa=" and retry :)
 
C

Chase Preuninger

you actually can even though I don't think most people know about it.

Just put 3 periods after it to tell it, and objs is just like an array
public void myMethod(Object... objs)
{
}
 
A

Arne Vajhøj

Martin said:
Is it possible in Java to write a method, which allows an arbitrary
number of formal parameters? I know it is possible in C# although I
forgot the syntax. So far, in Java I've only seen passing of a
Collection or Map of Arguments.

Java and C# has identical functionality with just slightly different
syntax.

using System;

public class VarArg
{
public static void Demo(params string[] mulval)
{
foreach(string val in mulval)
{
Console.WriteLine(val);
}
}
public static void Main(string[] args)
{
Demo("A");
Demo("1", "2");
Demo("X", "Y", "Z");
}
}

and

public class VarArg {
public static void demo(String... mulval) {
for(String val : mulval) {
System.out.println(val);
}
}
public static void main(String[] args) {
demo("A");
demo("1", "2");
demo("X", "Y", "Z");
}
}

Arne
 
M

Martin

(", which" is used for a non-restrictive clause.)

Hm, I used to use "that" most of the time until I read an article
about paper writing tips. There, it set "that" equal to ", which". I
feel there is a slight difference, but I'll consult a grammar page
about that.

Thanks everybody for your clarification on the vararg issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top