What does 'String...' mean? [duplicate]
Asked Answered
C

2

12

In the code:

public interface ProductInterface {

    public List<ProductVO> getProductPricing(ProductVO product, ProductVO prodPackage, String... pricingTypes) throws ServiceException;

}

What does

String... pricingTypes

mean? What type of construct is this?

Cancroid answered 11/8, 2013 at 20:49 Comment(2)
VarargsClements
Check the arguments tutorial to learn about varargs.Cheffetz
L
12

It is called varargs. It works for any type as long as it's the last argument in the signature.

Basically, any number of parameters are put into an array. This does not mean that it is equivalent to an array.

A method that looks like:

void foo(int bar, Socket baz...)

will have an array of Socket (in this example) called baz.

So, if we call foo(32, sSock.accept(), new Socket()) we'll find an array with two Socket objects.

Calling it as foo(32, mySocketArray) will not work as the type is not configured to take an array. However, if the signature is a varargs of arrays you can pass one or more arrays and get a two-dimensional array. For example, void bar(int bar, PrintStream[] baz...) can take multiple arrays of PrintStream and stick them into a single PrintStream[][].

Oddly enough, due to the fact that arrays are objects, Object... foo can take any number of arrays.

Lovelovebird answered 11/8, 2013 at 20:52 Comment(0)
N
4

It's a vararg - variable argument. You can pass a value of that type as many times as you want and the caller gets it as an array.

http://docs.oracle.com/javase/7/docs/technotes/guides/language/varargs.html

Nanon answered 11/8, 2013 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.