Class VarargsExamples

java.lang.Object
net.jrodolfo.java_evolution.java05.VarargsExamples

public class VarargsExamples extends Object
Demonstrates varargs, introduced in Java 5.
  • Constructor Details

    • VarargsExamples

      public VarargsExamples()
  • Method Details

    • joinLabels

      public String joinLabels(String... labels)
      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 other Iterable, 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

      public String joinLabels(Iterable<String> labels)
      Joins labels supplied by an existing Iterable.
      Parameters:
      labels - labels to join
      Returns:
      comma-separated labels
    • 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 value
      rest - optional remaining values
      Returns:
      maximum value