Class VarargsExamples
java.lang.Object
net.jrodolfo.java_evolution.java05.VarargsExamples
Demonstrates varargs, introduced in Java 5.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintcountValues(int... values) Shows that a varargs parameter is an array inside the method.joinLabels(Iterable<String> labels) Joins labels supplied by an existingIterable.joinLabels(String... labels) Joins zero or more labels.intmax(int first, int... rest) Returns the maximum value while requiring at least one argument.
-
Constructor Details
-
VarargsExamples
public VarargsExamples()
-
-
Method Details
-
joinLabels
Joins zero or more labels. Callers can pass individual arguments instead of manually creating an array.This is convenient for call sites such as
joinLabels("one", "two"). If the caller already has a collection or otherIterable, an iterable-based overload avoids forcing the caller to create an array only to call a varargs method.- Parameters:
labels- labels to join- Returns:
- comma-separated labels
-
joinLabels
-
countValues
public int countValues(int... values) Shows that a varargs parameter is an array inside the method.- Parameters:
values- values passed by the caller- Returns:
- number of values
-
max
public int max(int first, int... rest) Returns the maximum value while requiring at least one argument.The first parameter is mandatory, so callers cannot accidentally invoke this method with an empty argument list. The varargs parameter still lets callers pass any number of additional values.
- Parameters:
first- required first valuerest- optional remaining values- Returns:
- maximum value
-