Why Can’t Java Switch Use long? Uncovering the int‑Based Mechanics
This article explains why Java's switch statement only supports byte, short, int, char, enum, String and their wrapper types, detailing how each type is internally converted to int, why long is excluded, and what happens with null values.
Conclusion
The switch statement operates on int values; even enums and String cases are ultimately transformed into int. Because long exceeds the int range, it is not supported.
How Enum Types Become int
Two enums are defined – one with an int field and a String field, and another with only a String field. When compiled, the compiler generates a synthetic $VALUES array that holds all enum constants and a synthetic switch‑map array that maps enum.ordinal() to an int used by the switch statement.
public enum SexEnum {
MALE(1, "男"),
FEMALE(0, "女");
private int type;
private String name;
SexEnum(int type, String name) { this.type = type; this.name = name; }
}
public enum Sex1Enum {
MALE("男"),
FEMALE("女");
private String name;
Sex1Enum(String name) { this.name = name; }
}The test class compiled to:
class SwitchTest$1 {
static final int[] $SwitchMap$com$example$express$test$SexEnum;
static final int[] $SwitchMap$com$example$express$test$Sex1Enum = new int[Sex1Enum.values().length];
static {
try { $SwitchMap$com$example$express$test$Sex1Enum[Sex1Enum.FEMALE.ordinal()] = 1; } catch (NoSuchFieldError e) {}
try { $SwitchMap$com$example$express$test$Sex1Enum[Sex1Enum.MALE.ordinal()] = 2; } catch (NoSuchFieldError e) {}
$SwitchMap$com$example$express$test$SexEnum = new int[SexEnum.values().length];
try { $SwitchMap$com$example$express$test$SexEnum[SexEnum.MALE.ordinal()] = 1; } catch (NoSuchFieldError e) {}
try { $SwitchMap$com$example$express$test$SexEnum[SexEnum.FEMALE.ordinal()] = 2; } catch (NoSuchFieldError e) {}
}
}Thus the switch uses the ordinal‑based int mapping.
How String Becomes int
For String cases the compiler first switches on String.hashCode(). If different strings share the same hash code, an additional equals() check distinguishes them. The resulting branch index is then used in a second switch on the resolved int value.
public int stringSwitch(String s) {
byte var3 = -1;
switch (s.hashCode()) {
case -1554135584:
if (s.equals("helloWorld")) var3 = 2;
break;
case 165374702:
if (s.equals("ABCDFB123abc")) var3 = 1;
else if (s.equals("ABCDEa123abc")) var3 = 0;
break;
}
switch (var3) {
case 0: return 1;
case 1: return 2;
case 2: return 3;
default: return Integer.MAX_VALUE;
}
}Wrapper Types Support
Wrapper types such as Integer are supported via automatic unboxing. The compiled bytecode calls intValue() before the switch comparison.
public int integerSwitch(Integer c) {
switch (c.intValue()) {
case 1: return 1;
case 2: return 2;
default: return -1;
}
}If a null wrapper is passed, a NullPointerException occurs because the unboxing operation cannot be performed.
Illustrations
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
