Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:15:29+00:00 2026-06-18T08:15:29+00:00

Why does this, public class Bar { public static void main(String[] args) { int

  • 0

Why does this,

public class Bar 
{
    public static void main(String[] args) 
    {
        int i = 1;

        switch(i)
        {
            case 0:
            case 1:
            case 2:
                System.out.println("Case 2 being executed");
                break;
            default:
                break;
        }
    }
}

output this,

Case 2 being executed

?

How is it even possible to enter the case block for an input value of 2 when the input value is explicitly 1? Note that I’m aware I can avoid this behavior by adding a break statement in the case block for 1.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:15:30+00:00Added an answer on June 18, 2026 at 8:15 am

    How is it even possible to enter the case block for an input value of
    2 when the input value is explicitly 1?

    This behaviour is called fall-through which is quite common mistake with beginners working with switch-case. Actually, case 1: does execute first. But, since there is no break statement in case 1, your switch-case goes onto executing the next cases, until it finds a break statement. So, it will even execute the code for case 2: and hence the output. And then it breaks after executing case 2, as it encounters a break.

    So, change your swich-case to: –

    switch(i)
    {
            case 0: break;
            case 1: break;
            case 2:
                System.out.println("Case 2 being executed");
                break;
            default:
                break;
    }
    

    to see the intended behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know why this doesn't compile? public class ArrayCompare{ public static void main
I would like to have a mapping function that does this: public static void
When running java -javaagent:ObjectSizeFetcherAgent.jar PersistentTime I get 24 when ObjectSizeFetcherAgent does this public class
Why does this code not print an exception stack trace? public class Playground {
What does a syntax like this mean in C#? public abstract class HostFactory<TApp> :
The browser does this by calling public void emulateShiftHeld() method on the WebView which
/// <summary> /// This method does something... /// </summary> public void DoSomething() { //
I have an arraylist of entities. The Entity class has public void update(int delta,
public class Base { public Base() { x = 0; bar(); } public Base(int
I'm trying to do it class Foo { static int ReadSelfFromBase(void *userarg, int argc,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.