Java switch不支持基本类型long

原文

内容摘自 « The Java Virtual Machine Specification Java SE 10 Edition » PDF – Page57

3.10. Compiling Switches

Compilation of switch statements uses the tableswitch and lookupswitch 
instructions. The tableswitch instruction is used when the cases of the 
switch can be efficiently represented as indices into a table of target 
offsets. The default target of the switch is used if the value of the 
expression of the switch falls outside the range of valid indices. For 
instance:

    int chooseNear(int i) {
        switch (i) {
            case 0:  return  0;
            case 1:  return  1;
            case 2:  return  2;
            default: return -1;
        }
    }

compiles to:

    Method int chooseNear(int)
    0   iload_1             // Push local variable 1 (argument i)
    1   tableswitch 0 to 2: // Valid indices are 0 through 2
          0: 28             // If i is 0, continue at 28
          1: 30             // If i is 1, continue at 30
          2: 32             // If i is 2, continue at 32
          default:34        // Otherwise, continue at 34
    28  iconst_0            // i was 0; push int constant 0...
    29  ireturn             // ...and return it
    30  iconst_1            // i was 1; push int constant 1...
    31  ireturn             // ...and return it
    32  iconst_2            // i was 2; push int constant 2...
    33  ireturn             // ...and return it
    34  iconst_m1           // otherwise push int constant -1...
    35  ireturn             // ...and return it

The Java Virtual Machine's tableswitch and lookupswitch instructions 
operate only on int data. Because operations on byte, char, or short 
values are internally promoted to int, a switch whose expression 
evaluates to one of those types is compiled as though it evaluated to 
type int. If the chooseNear method had been written using type short, the 
same Java Virtual Machine instructions would have been generated as when 
using type int. Other numeric types must be narrowed to type int for use 
in a switch.

解释

  1. 上文明确指出JVMtableswitch
    lookupswitch
    指令只能操作int数据:

    The Java Virtual Machine’s tableswitch and lookupswitch instructions operate only on int data.

  2. 同时表明在 switch
    中执行 byte
    char
    short
    会隐式向上转型为基本类型 int

参考链接

  • why-cant-java-switch-over-the-primitive-long

  • Java Language and Virtual Machine Specifications

  • 上一篇

    ViewPager禁止滚动

原文 

https://phantomvk.github.io/2018/06/11/Java_Switch_long/

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » Java switch不支持基本类型long

分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址