Interview Q&A · Java · 5 questions in this topic

Varargs and switch on Strings — Java Interview Q&A

All Java topics

Variable-arity methods and the switch statement, including String support and the modern switch expression. Part of Interfaces, Enums & Features · 294 questions in the Java bank.

What are varargs?

Answer

Varargs, added in Java 5, let the last parameter of a method accept a variable number of arguments, written as Type... name. Inside the method the parameter behaves like an array, and a call with zero arguments produces an empty array rather than null. The flexibility has a cost: combining varargs with overloading can create ambiguous calls, so varargs overloads should be designed carefully.

Key points

  1. 1Only the final parameter may be a varargs parameter, and there can be only one of them.
  2. 2Callers may pass individual values, an array, or nothing at all.
  3. 3Overloads that differ only by varargs versus array or by nearby numeric types can confuse the compiler.

Example

Example.javajava
static int sum(int... values) {
    int total = 0;
    for (int v : values) {
        total += v;
    }
    return total;
}
sum(1, 2, 3); // 6
sum();        // 0: values is an empty array

Can switch work on Strings?

Answer

Yes. Since Java 7 a switch may dispatch on String values, comparing them with equals semantics driven by hash codes rather than reference identity. Switching on a null String throws a NullPointerException, so a null check is required first. Since Java 14 the modern switch expression with arrow labels offers a more compact form that can also produce a value.

Example

Example.javajava
String day = "MON";
int meetings;
switch (day) {
    case "MON":
        meetings = 3;
        break;
    case "FRI":
        meetings = 1;
        break;
    default:
        meetings = 0;
}
int m = switch (day) {
    case "MON" -> 3;
    case "FRI" -> 1;
    default -> 0;
};

💡 Mention that case labels must be compile-time constants and that null needs a guard to show depth beyond a bare yes.

What are the rules for declaring and calling varargs methods?

Answer

A varargs parameter is written T... name, must be the last parameter, and at most one can appear in a signature. Inside the method it is an ordinary array, so name.length and iteration behave normally, and the caller may pass zero arguments, several, or an explicit array. Zero arguments produce a zero-length array, never null. Ambiguity appears with overloads — f(int...) versus f(int, int) makes some calls ambiguous — and passing null explicitly is legal but usually a bug.

Key points

  1. 1Exactly one varargs parameter, and it must be last.
  2. 2No arguments means an empty array, not null.
  3. 3An explicit array is passed by reference, not copied.
  4. 4Overloading with varargs can create ambiguous calls.

Example

Example.javajava
static int sum(int... xs) { int s = 0; for (int x : xs) s += x; return s; }
sum();          // 0
sum(1, 2, 3);   // 6
sum(new int[]{4, 5}); // 9

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits