Interview Q&A · Java · 5 questions in this topic
Varargs and switch on Strings — Java Interview Q&A
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
- 1Only the final parameter may be a varargs parameter, and there can be only one of them.
- 2Callers may pass individual values, an array, or nothing at all.
- 3Overloads that differ only by varargs versus array or by nearby numeric types can confuse the compiler.
Example
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
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
- 1Exactly one varargs parameter, and it must be last.
- 2No arguments means an empty array, not null.
- 3An explicit array is passed by reference, not copied.
- 4Overloading with varargs can create ambiguous calls.
Example
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
Can you switch on a String, and how does it work?
Answer
Yes, since Java 7. The compiler desugars the switch into a lookup on hashCode() plus equals() comparisons, so it is efficient but has two consequences worth knowing: a null selector throws NullPointerException before any case runs, and matching is case-sensitive. For a small set of constants an if/else chain with equals is equivalent; for a closed set of values an enum is safer because the compiler can check exhaustiveness.
Key points
- 1String switch exists since Java 7 and uses hashCode plus equals.
- 2Null throws NullPointerException.
- 3Comparison is case-sensitive.
- 4Case labels must be compile-time constants.
What switch forms does modern Java support?
Answer
Classic switch statements with colon labels and fall-through still work. Java 14 added arrow labels (case X -> …) that never fall through, and switch expressions that yield a value. Java 17 added pattern matching for switch as a preview and Java 21 finalised type patterns, guarded patterns and a null case, which together let one switch handle a sealed hierarchy exhaustively. Prefer the arrow form in new code: it removes accidental fall-through and makes exhaustiveness checkable.
Key points
- 1Arrow labels case X -> … do not fall through.
- 2Switch expressions must cover every case or provide default.
- 3Type patterns with when guards arrived in Java 21.
- 4Exhaustive switch over a sealed type needs no default.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission