嵌套五个条件运算符

如何解决嵌套五个条件运算符

| 是否可以在Java中嵌套5个以上的“条件运算符”。我问是因为似乎在尝试编译此代码时会导致编译器异常:
public Object getValue() {
        return
            number  != null ? number    :
            string  != null ? string    :
            bool    != null ? bool      :
            date    != null ? date      :
            list    != null ? list      :
            null;
}
我将其范围缩小到此代码,因为如果我注释掉最后一行,则似乎可以正常编译。
public Object getValue() {
        return
            number  != null ? number    :
            string  != null ? string    :
            bool    != null ? bool      :
            date    != null ? date      :
//        list    != null ? list      :
            null;
}
有人知道这是否是java编译器的局限性,还是我跳到错误的结论,如果有人可以尝试重现这一点,那就太好了。如果有人感兴趣,我已经在此处https://gist.github.com/919284复制并发布了来自编译器的堆栈跟踪。 请注意,很可能是编译器中的错误而不是我的代码,因为输出显示“请在Java Developer Connect站点上提交错误”(或类似内容)。我在这里问是因为我不确定该错误报告将包含什么。 编辑: 克里斯·L(Chris L)转载了这个     

解决方法

我转载了您的错误(在Mac上使用Sun JDK 1.6.0_24)。我将您的课程简化为:
import java.util.ArrayList;
import java.util.Date;

public class Test3 {

    private Number number;
    private String string;
    private Boolean bool; // Replace Boolean with Object,and it compiles!
    private Date date;
    private ArrayList<String> list; // Replace ArrayList with List,and it
                                    // compiles!

    public Object getValue() {
        return number != null ? number :
               string != null ? string :
               bool != null ? bool :
               date != null ? date :
               list != null ? list :
               null;
    }
}
我的堆栈跟踪基本上与您的相同。 (顺便说一下,它与GWT无关。)
An exception has occurred in the compiler (1.6.0_24). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
java.lang.AssertionError
    at com.sun.tools.javac.jvm.Code$State.forceStackTop(Code.java:1688)
    at com.sun.tools.javac.jvm.Gen.visitConditional(Gen.java:1679)
    at com.sun.tools.javac.tree.JCTree$JCConditional.accept(JCTree.java:1021)
    at com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:818)
    at com.sun.tools.javac.jvm.Gen.visitConditional(Gen.java:1678)
    at com.sun.tools.javac.tree.JCTree$JCConditional.accept(JCTree.java:1021)
    at com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:818)
    at com.sun.tools.javac.jvm.Gen.visitConditional(Gen.java:1678)
    at com.sun.tools.javac.tree.JCTree$JCConditional.accept(JCTree.java:1021)
    at com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:818)
    at com.sun.tools.javac.jvm.Gen.visitReturn(Gen.java:1626)
    at com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1138)
    at com.sun.tools.javac.jvm.Gen.genDef(Gen.java:665)
    at com.sun.tools.javac.jvm.Gen.genStat(Gen.java:700)
    at com.sun.tools.javac.jvm.Gen.genStat(Gen.java:686)
    at com.sun.tools.javac.jvm.Gen.genStats(Gen.java:737)
    at com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1013)
    at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:739)
    at com.sun.tools.javac.jvm.Gen.genDef(Gen.java:665)
    at com.sun.tools.javac.jvm.Gen.genStat(Gen.java:700)
    at com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:893)
    at com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:866)
    at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:639)
    at com.sun.tools.javac.jvm.Gen.genDef(Gen.java:665)
    at com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2198)
    at com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:617)
    at com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1289)
    at com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1259)
    at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:765)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:730)
    at com.sun.tools.javac.main.Main.compile(Main.java:353)
    at com.sun.tools.javac.main.Main.compile(Main.java:279)
    at com.sun.tools.javac.main.Main.compile(Main.java:270)
    at com.sun.tools.javac.Main.compile(Main.java:69)
    at com.sun.tools.javac.Main.main(Main.java:54)
    ,我只能在eclipse 3.5和javac 1.6.0_u24中为我确认此编译是否正确:
public class Test {
    Object number=null,string=null,bool=null,date=null,list=null;

    public Object getValue() {
        return
            number  != null ? number    :
            string  != null ? string    :
            bool    != null ? bool      :
            date    != null ? date      :
            list    != null ? list      :
            null;
    }
}
    ,这在ideone上编译良好:
    public static void main (String[] args) throws java.lang.Exception
    {
        Object number = null;
        Object string = null;
        Object list = null;
        Object bool = null;
        Object date = null;


        Object o = 
        number  != null ? number    :
        string  != null ? string    :
        bool    != null ? bool      :
        date    != null ? date      :
        list    != null ? list      :
        null;

    }
仔细检查是否以方法内部可以访问的方式声明了“ 6”。 可能是Java编译器中的错误。我建议您将Java更新到最新和最好的版本(如果有的话)并进行复制。您可以根据需要安装许多不同版本的Java。     ,我认为语法上正确无限制。我猜想Java编译器只会像深层if / else if嵌套那样扩展其解析树。     ,没有如此低的限制。一个方法必须编译为少于64KB的字节码。 我很好地整理了您的示例。您有没有一个字段的任何原因吗? 编辑:添加了setter来检查有效类型。
public class Holder implements Serializable {
    Serializable value;

    public void setValue(Number value) {
        this.value = value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setValue(Boolean value) {
        this.value = value;
    }

    public void setValue(Date value) {
        this.value = value;
    }

    public <L extends List & Serializable> void setValue(L value) {
        this.value = value;
    }

    public Serializable getValue() {
        return value;
    }
}
    ,我知道这是一篇过时的文章,但是我最近的经验可能会为感兴趣的人提供一些启发。有一点要注意。 基本上,我通过在其他类之一中实现Comparable来“破坏”一些现有代码。这是一个精简版本,可生成相同的“编译器中发生异常...” 如果嵌套条件中的表达式少于5个,或者USDollars类未实现Comparable,则此代码编译。
    public class TestHit
      {
      protected final String fSymbol;
      protected final long fTime;
      protected final USDollars fBasePrice;

      public TestHit(String aSymbol,long aTime,int aBasePrice)
        {
        fSymbol = aSymbol;
        fTime = aTime;
        fBasePrice = new USDollars(aBasePrice);
        }

      public Object field(int aIndex)
        {
        return (aIndex == 0)? fSymbol
             : (aIndex == 1)? fTime
             : (aIndex == 2)? fBasePrice
             : (aIndex == 3)? new Integer(4)   // comment out this line and it compiles
             : \"?\";
        }
      }

    final class USDollars
      implements Comparable<USDollars> // comment out this line and it compiles
      {
      private int cents;

      public USDollars() { this(0); }
      public USDollars(int cents) { this.cents = cents; }
      public USDollars(int dollars,int cents) { this(cents + 100*dollars); }

      public int cents() { return cents; }

    // @Override
      public int compareTo(USDollars other) { return this.cents - other.cents; }
      }
顺便说一句,一个快速的解决方法是按如下方式修改代码(难看,但是可以):
      public Object field(int aIndex)
        {
        if (aIndex == 2)
           return fBasePrice;
        return (aIndex == 0)? fSymbol
             : (aIndex == 1)? fTime
             : (aIndex == 3)? new Integer(4)   // comment out this line and it compiles
             : \"?\";
        }
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-