How is String in switch statement more efficient than corresponding if-else statement?

前端 未结 3 1577
暗喜
暗喜 2020-11-28 06:46

Java documentation says

The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chaine

3条回答
  •  萌比男神i
    2020-11-28 07:38

    This is a bytecode fragment generated from example in the docs:

     INVOKEVIRTUAL java/lang/String.hashCode ()I
        LOOKUPSWITCH
          -2049557543: L2
          -1984635600: L3
          -1807319568: L4
    

    using LOOKUPSWITCH has better perfomance compared to if-else logic

提交回复
热议问题