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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:41:11+00:00 2026-05-26T14:41:11+00:00

I am currently using a switch statement to handle types of incoming messages of

  • 0

I am currently using a switch statement to handle types of incoming messages of which there are 20 or so different cases. Some of these cases are orders of magnitude more likely to occur than others.

Is the hotspot compiler able to optimise the order of examining cases to find the correct case to execute or should I structure my code so that the most common cases appear first:

switch(messageType)
{
    case MOST_COMMON:
        // handle it
        break;

...
    case LEAST_COMMON:
        // handle it
        break;
}

All cases are mutually exclusive.

Would I be better off using the strategy pattern and a Map lookup on message type?

Performance is the key concern as I am handling thousands of messages per second and am trying to cut down on object creation and method call overhead.

Many thanks,

Chris

Edit:
Thanks for the pointers.
messageType is an int with a tight range of values so it looks like it will compile to the “tableswitch” bytecode so no need to reorder the cases.

Relevant part of JVM spec is here http://java.sun.com/docs/books/jvms/second_edition/html/Compiling.doc.html#14942

  • 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-05-26T14:41:12+00:00Added an answer on May 26, 2026 at 2:41 pm

    Unless you are sure that this switch statement is causing you performance problems, then I would suggest that you’re optimizing prematurely. Also, check out the accepted answer to this question.

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

Sidebar

Related Questions

I'm using a switch/case statement to handle some updates for a deployed application. Basically,
There are two different UserControls which share some common Properties. What I'd like to
currently i'm attempting to using a switch statement to change between time zone with
I currently using android NDK to write some native code in C. I have
I have the following code which I am are currently using .... Basically, this
I currently have a page that allows a user to enter different types of
I'm currently using Crystal Reports and I'd like to switch to Jasper. To ease
Currently I'm creating my XML-RPC using (xml-rpc.net) interfaces statically with the following statement: [XmlRpcUrl(http://dillieodigital.wordpress.com/xmlrpc.php)]
I'm currently using the following block of code to switch between two divs. $('.btn-my-projects').click(function(e)
Currently using the HTTPServletRequest class and specifically the .getQueryString method to retrieve an inputted

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.