switch-case : A Java Interview Question

Serdar A.
2 min readJun 18, 2023

Hi everyone;

I wanted to write an article about a situation that caught my attention while I was working on interviews recently.

Let’s start by asking the output of the following code block first.

public class SwitchCaseExample {

public static void main(String[] args) {

long num = 5;


private static void checkValue(long num) {

switch (num) {
case 4 -> System.out.println("4");
case 5 -> System.out.println("5");
default -> throw new ClassCastException("can not accept");



What do you expect to happen when we run this code block?

A) 5
B) Runtime Error
C) 4
D) Compile Time Error
E) ClassCastException

Simply copy and paste the question into an IDE to find the answer.

Answer is Compile Time Error


In Java, switch works only with 4 primitives and their wrappers, as well as with the enum type and String class.

This is

byte — Byte
short — Short
char — Character
int — Integer

I want to share this info for you

Thanx to reading



Serdar A.

Senior Software Developer & Architect at Havelsan Github: https://github.com/serdaralkancode #Java & #Spring & #BigData & #React & #ReactNative & #JS #Microserv