---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 21 Edition"
java_version: 21
chapter: "Chapter 14. Blocks, Statements, and Patterns"
section: "14.11.1.2"
heading: "14.11.1.2. Determining which Switch Label Applies at Run Time"
parent: "14.11.1. Switch Blocks"
source: https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.11.1.2
id: "14.11.1.2"
url: https://jvm.obfuscated.software/jls/se21/14.11.1.2.html
raw: https://jvm.obfuscated.software/jls/se21/14.11.1.2.md
---

# 14.11.1.2. Determining which Switch Label Applies at Run Time

Both the execution of a `switch` statement ([§14.11.3](14.11.3.md)) and the evaluation of a `switch` expression ([§15.28.2](15.28.2.md)) need to determine if a switch label associated with the switch block *applies* to the value of the selector expression. This proceeds as follows:

1. If the value is the null reference, then a `case` label with a `null` literal applies.

2. If the value is not the null reference, then we determine the first (if any) `case` label in the switch block that applies to the value as follows:

   - A `case` label with a `case` constant `c` applies to a value of type `Character`, `Byte`, `Short`, or `Integer`, if the value is first subjected to unboxing conversion ([§5.1.8](5.1.8.md)) and the constant `c` is equal to the unboxed value.

     Any unboxing conversion will complete normally as the value being unboxed is guaranteed not to be the null reference.

     Equality is defined in terms of the `==` operator ([§15.21](15.21.md)).

   - A `case` label with a `case` constant `c` applies to a value that is of type `char`, `byte`, `short`, `int`, or `String` or an enum type if the constant `c` is equal to the value.

     Equality is defined in terms of the `==` operator unless the value is a `String`, in which case equality is defined in terms of the `equals` method of class `String`.

   - Determining that a `case` label with a `case` pattern `p` applies to a value proceeds first by checking if the value matches the pattern `p` ([§14.30.2](14.30.2.md)).

     If pattern matching completes abruptly then the process of determining which switch label applies completes abruptly for the same reason.

     If pattern matching succeeds and the `case` label is unguarded then this `case` label applies.

     If pattern matching succeeds and the `case` label is guarded, then the guard is evaluated. If the result is of type `Boolean`, it is subjected to unboxing conversion ([§5.1.8](5.1.8.md)).

     If evaluation of the guard or the subsequent unboxing conversion (if any) completes abruptly for some reason, the process of determining which switch label applies completes abruptly for the same reason.

     Otherwise, if the resulting value is `true` then the `case` label applies.

   - A `case` `null,` `default` label applies to every value.

3. If the value is not the null reference, and no `case` label applies according to the rules of step 2, but there is a `default` label associated with the switch block, then the `default` label applies.

A single `case` label can contain several `case` constants. The label applies to the value of the selector expression if any one of its constants is equal to the value of the selector expression. For example, in the following code, the `case` label applies if the enum variable `day` is either one of the enum constants shown:

```
switch (day) {
    ...
    case SATURDAY, SUNDAY :
        System.out.println("It's the weekend!");
        break;
    ...
}
```

If a `case` label with a `case` pattern applies, then this is because the process of pattern matching the value against the pattern has succeeded ([§14.30.2](14.30.2.md)). If a value successfully matches a pattern then the process of pattern matching initializes any pattern variables declared by the pattern.

In C and C++ the body of a `switch` statement can be a statement and statements with `case` labels do not have to be immediately contained by that statement. Consider the simple loop:

```
for (i = 0; i < n; ++i) foo();
```

where `n` is known to be positive. A trick known as *Duff's device* can be used in C or C++ to unroll the loop, but this is not valid code in the Java programming language:

```
int q = (n+7)/8;
switch (n%8) {
    case 0: do { foo();    // Great C hack, Tom,
    case 7:      foo();    // but it's not valid here.
    case 6:      foo();
    case 5:      foo();
    case 4:      foo();
    case 3:      foo();
    case 2:      foo();
    case 1:      foo();
            } while (--q > 0);
}
```

Fortunately, this trick does not seem to be widely known or used. Moreover, it is less needed nowadays; this sort of code transformation is properly in the province of state-of-the-art optimizing compilers.
