concept case label in category java

appears as: case labels, case label, The case labels
Java SE 11 Programmer I Certification Guide

This is an excerpt from Manning's book Java SE 11 Programmer I Certification Guide.

Explanation: Option (a) is correct. Compile-time constants, including expressions, are permissible in the case labels.

Option (b) is incorrect. The case labels should be compile-time constants. A non-final variable isn’t a compile-time constant because it can be reassigned a value during the course of a class’s execution. Although the previous class doesn’t assign a value to it, the compiler still treats it as a changeable variable.

Option (c) is correct. The value specified in the case labels should be assignable to the variable used in the switch construct. You may think that 10/3 will return a decimal number, which can’t be assigned to the variable num, but this operation discards the decimal part and compares 3 with the variable num.

OCA Java SE 8 Programmer I Certification Guide

This is an excerpt from Manning's book OCA Java SE 8 Programmer I Certification Guide.

A switch statement can define multiple case labels within its switch block but only a single default label. The default label executes when no matching value is found in the case labels. A break statement is used to exit a switch statement, after the code completes its execution for a matching case.

You’re constrained in a couple of ways when it comes to the value that can be passed to the case label in a switch statement, as the following subsections explain.