Welcome to the second article in my series on preparing for the Java 21 certification.
Over the next several posts, we’ll walk through the most important language changes, APIs, and tricky details you’ll need to know for the exam.
In this article, we’ll focus on switch expressions, utilizing pattern matching for switches and enums.
To make things practical, we’ll analyze a question in the style of the certification exam.
❓ Question
What is the answer to this code?
enum Card {
CLUB, DIAMOND, HEART, SPADE;
}
public class Question3 {
public static void main(String[] args) {
Card card = Card.CLUB;
printCardColor(card);
}
private static void printCardColor(Card card) {
String color = null;
if (card == Card.HEART || card == Card.DIAMOND) {
color = "red";
}
if (card == Card.SPADE || card == Card.CLUB) {
color = "black";
}
System.out.println(color);
}
}
Which option can replace the printCardColor
method? Choose the right answer.
A)
private static void printCardColor(Card card) {
String color = null;
if (card != null) {
color = switch (card.ordinal()) {
case 0, 1 -> "red";
case 2, 3 -> "black";
};
}
System.out.println(color);
}
B)
private static void printCardColor(Card card) {
String color = switch (Card.valueOf(card.toString())) {
case DIAMOND, HEART -> "red";
case SPADE, CLUB -> "black";
};
System.out.println(color);
}
C)
private static void printCardColor(Card card) {
String color = switch (card) {
case HEART, DIAMOND -> "red";
case SPADE, CLUB -> "black";
default -> null;
};
System.out.println(color);
}
D)
private static void printCardColor(Card card) {
String color = switch (card) {
case null -> null;
case HEART, DIAMOND -> "red";
case SPADE, CLUB -> "black";
};
System.out.println(color);
}
🔎 Explanation
A) Using ordinal
Problem: This code does not compile because the
switch
onint
(fromcard.ordinal()
) is not exhaustive — there’s nodefault
branch.Fix: Add a
default
branch to make it compile, but it’s still a bad practice for enums.
B) Using valueOf()
Problem: If
card
isnull
,card.toString()
throws a NullPointerException.
C) Using switch with default
Problem: The
switch
is exhaustive for allCard
constants, sodefault
is technically unnecessary. However, becausecard
might benull
, a NullPointerException is thrown before entering the switch, making this option incorrect for robust code.
D) Using switch with null case
Correct Solution. In this case, we have a switch null‑safe, and the same result as the previous code.
✅ Correct Answer
D
Did you get it right? Don’t worry if you missed some; this is a hard question. The goal here is to expose you to several tricky details that often show up in the Java 21 certification exam.
⚡ Next in this series: we’ll dive into Stream API.
📬 Stay tuned:
If you don’t want to miss the upcoming parts of this Java 21 Certification series, hit the Subscribe button below.
Excellent point! I normally handle this kind of validation with a guard clause, but this approach is a much better way to achieve concision. Thanks for sharing, Thiago!