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 7892545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:53:52+00:00 2026-06-03T06:53:52+00:00

PMD tells me A switch with less than 3 branches is inefficient, use a

  • 0

PMD tells me

A switch with less than 3 branches is inefficient, use a if statement
instead.

Why is that? Why 3? How do they define efficiency?

  • 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-03T06:53:53+00:00Added an answer on June 3, 2026 at 6:53 am

    Because a switch statement is compiled with two special JVM instructions that are lookupswitch and tableswitch. They are useful when working with a lot of cases but they cause an overhead when you have just few branches.

    An if/else statement instead is compiled into typical je jne … chains which are faster but require many more comparisons when used in a long chain of branches.

    You can see the difference by looking at byte code, in any case I wouldn’t worry about these issues, if anything could become a problem then JIT will take care of it.

    Practical example:

    switch (i)
    {
      case 1: return "Foo";
      case 2: return "Baz";
      case 3: return "Bar";
      default: return null;
    }
    

    is compiled into:

    L0
     LINENUMBER 21 L0
     ILOAD 1
     TABLESWITCH
       1: L1
       2: L2
       3: L3
       default: L4
    L1
     LINENUMBER 23 L1
    FRAME SAME
     LDC "Foo"
     ARETURN
    L2
     LINENUMBER 24 L2
    FRAME SAME
     LDC "Baz"
     ARETURN
    L3
     LINENUMBER 25 L3
    FRAME SAME
     LDC "Bar"
     ARETURN
    L4
     LINENUMBER 26 L4
    FRAME SAME
     ACONST_NULL
     ARETURN
    

    While

    if (i == 1)
      return "Foo";
    else if (i == 2)
      return "Baz";
    else if (i == 3)
      return "Bar";
    else
      return null;
    

    is compiled into

    L0
     LINENUMBER 21 L0
     ILOAD 1
     ICONST_1
     IF_ICMPNE L1
    L2
     LINENUMBER 22 L2
     LDC "Foo"
     ARETURN
    L1
     LINENUMBER 23 L1
    FRAME SAME
     ILOAD 1
     ICONST_2
     IF_ICMPNE L3
    L4
     LINENUMBER 24 L4
     LDC "Baz"
     ARETURN
    L3
     LINENUMBER 25 L3
    FRAME SAME
     ILOAD 1
     ICONST_3
     IF_ICMPNE L5
    L6
     LINENUMBER 26 L6
     LDC "Bar"
     ARETURN
    L5
     LINENUMBER 28 L5
    FRAME SAME
     ACONST_NULL
     ARETURN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PMD suggested in my GWT project that I should change my switch to multiple
I'd like to use a custom PMD ruleset file for my build. Basically, I
With PMD, if you want to ignore a specific warning, you can use //
We know that PMD is integrated with eclipse. And i want to customize it.
I wish to add a PMD check to ensure that a class does not
I need to use System.exit(0) in an application. Eclipse has the PMD plugin installed
I have taken a look at the PMD api which I want to use
I use PMD to check my code. It gives me very useful hints in
Could someone suggest a PMD xpath rule to warn that calling the equals() method
That's about PMD static analyzer's feature: Copy-Paste Detector. Yes, I read http://pmd.sourceforge.net/cpd.html thoroughly. But

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.