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

  • Home
  • SEARCH
  • 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 8017361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:49:19+00:00 2026-06-04T20:49:19+00:00

In Java 7 a string object can be in the expression of a switch

  • 0

In Java 7 a string object can be in the expression of a switch statement. Can someone explain the below statement from official documentation?

The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.

  • 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-04T20:49:21+00:00Added an answer on June 4, 2026 at 8:49 pm

    Java Code

    Having two versions of a class, e.g.

    With if-then-else:

    public class IfThenElseClass {
        public static void main(String[] args) {
            String str = "C";
            if ("A".equals(str)) {
    
            } else if ("B".equals(str)) {
    
            } else if ("C".equals(str)) {
    
            }
        }
    }
    

    With switch:

    public class SwitchClass {
        public static void main(String[] args) {
            String str = "C";
            switch (str) {
                case "A":
                    break;
                case "B":
                    break;
                case "C":
                    break;
            }
        }
    }
    

    Bytecode

    Let’s take a look at the bytecode. Getting the bytecode for if-then-else version:

    Compiled from "CompileSwitch.java"
    public class CompileSwitch {
      public CompileSwitch();
        Code:
           0: aload_0
           1: invokespecial #8  // Method java/lang/Object."<init>":()V
           4: return
    
      public static void main(java.lang.String[]);
        Code:
           0: ldc           #16 // String C
           2: astore_1
           3: ldc           #18 // String A
           5: aload_1
           6: invokevirtual #20 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
           9: ifne          28
          12: ldc           #26 // String B
          14: aload_1
          15: invokevirtual #20 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
          18: ifne          28
          21: ldc           #16 // String C
          23: aload_1
          24: invokevirtual #20 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
          27: pop
          28: return
    }
    

    Getting the bytecode for switch version:

    Compiled from "CompileSwitch.java"
    public class CompileSwitch {
      public CompileSwitch();
        Code:
           0: aload_0
           1: invokespecial #8 // Method java/lang/Object."<init>":()V
           4: return
    
      public static void main(java.lang.String[]);
        Code:
           0: ldc           #16 // String C
           2: astore_1
           3: aload_1
           4: dup
           5: astore_2
           6: invokevirtual #18 // Method java/lang/String.hashCode:()I
           9: lookupswitch  { // 3
                        65: 44
                        66: 56
                        67: 68
                   default: 77
              }
          44: aload_2
          45: ldc           #24 // String A
          47: invokevirtual #26 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
          50: ifne          77
          53: goto          77
          56: aload_2
          57: ldc           #30 // String B
          59: invokevirtual #26 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
          62: ifne          77
          65: goto          77
          68: aload_2
          69: ldc           #16 // String C
          71: invokevirtual #26 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
          74: ifne          77
          77: return
    }
    

    Conclusion

    • In the first version compares the string by calling the equals method for each condition, until it is found.

    • In the second version is obtained first hashCode of the string. Then this is compared with the values ​​hashCode each case. See the lookupswitch. If any of these values ​​is repeated just happens to run the code for the case. Otherwise, call the equals method of the cases tied. This is much faster than ever call the equals method only.

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

Sidebar

Related Questions

As far as I know, in Java I can Object o = new String(abc)
The JDK documentation for java.lang.String.hashCode() famously says: The hash code for a String object
I have a Java Properties object that I load from an in-memory String ,
How-Do/Can I set the value of a String object in Java (without creating a
Consider these methods from java.lang.String /** * Returns the string representation of the <code>Object</code>
How can I create a String object from a byte array byte arr[MAX_SIZE]; //
I am trying to get a java.net.URI object from a String . The string
I want to read Object from string expression. eg: I have following string: (3:2,1)
I have a simple java String object and wanna serialize/deserialize it, using XStream. Serialization
How to print the reverse of the String java is object orientated language without

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.