LearnFrenzy
Sign in Take a free test

Control Flow Statements · Find the Error · Question 5

Error: switching on a long Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        long id = 3L;
        switch (id) {
            case 1L -> System.out.println("admin");
            case 3L -> System.out.println("guest");
            default -> System.out.println("user");
        }
    }
}
  • A Arrows are not allowed in statements
  • B long is not a permitted selector type for constant case labels; use int, or if/else for long values Correct
  • C default must be last
  • D id must be final
Correct Answer: Option B

The same is true for float, double and boolean.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links