这是为什么Java的code执行多个return语句 [英] Why is this java code executing multiple return statements

查看:102
本文介绍了这是为什么Java的code执行多个return语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在执行过程中,而在debuger运行内外返回说明书的函数,如果再在函数的末尾的返回说明书。

我很困惑我想返回说明书返回的值,并停止执行该方法的流程?

我找到了工作,但想了解为什么它是这样做的。

如在下面的例子。 JSON的失败对答案的ADN块createapprove。它得到创建失败,RET变为真(由于JSON解析)。返回RETVAL运行,然后code跳转到最后返回RETVAL并执行,作为回报功能。

我通过code中的Ecipse走在调试模式在Android模拟器中运行的程序。

 公共静态ComModelFromServer createModelFromJSON(JSON字符串)抛出JSONInvalidException {
     布尔RET = FALSE;
     ComModelFromServer RETVAL = NULL;     //回答座
     接听=新AnswerAsset();
     RET = a.tryToParseFromJSON(JSON);
     如果(RET ==真){
        RETVAL =一个;
        返回RETVAL;
    }    创建CR =新CreateApprove();
    RET = cr.tryToParseFromJSON(JSON);
    如果(RET ==真){
        RETVAL = CR;
        返回RETVAL;
    }    CR =新CreateFail();
    RET = cr.tryToParseFromJSON(JSON);
    如果(RET ==真){
        RETVAL = CR;
        返回RETVAL;
    }    如果(RET = = FALSE){
        抛出新JSONInvalidException(无法创建JSON模式);
    }    返回RETVAL;
}


解决方案

有可能是没有任何问题与此有关。编译器可能编译返回RETVAL 如果语句转换跳转到一个跳转里面的返回在函数的结束,这的有一个返回RETVAL 语句。显示奇流量控制调试器的的意思是有什么不妥。

I have a function that during execution while in the debuger runs both the return statment inside the if and then the return statment at the end of the function.

I'm confused I thought the return statment returned the value and stopped the flow of execution in the method?

I've got it to work, but would like to understand why it is doing this.

As an example in the below. The json will fail on the answer adn the createapprove block. It gets to create fail and ret becomes true (As the json parsed). The return retval runs and then the code jumps down to the final return retval and executes that as the return for the function.

I'm walking through the code in ecipse in debug mode with the program running on the android emulator.

public static ComModelFromServer createModelFromJSON(String json) throws JSONInvalidException {     
     boolean ret = false;
     ComModelFromServer retval = null;

     //Answer Block
     Answer a = new AnswerAsset();
     ret = a.tryToParseFromJSON(json);
     if(ret == true) {
        retval = a;
        return retval;
    }

    Create cr = new CreateApprove();
    ret = cr.tryToParseFromJSON(json);
    if(ret == true) {
        retval = cr;
        return retval;
    }

    cr = new CreateFail();
    ret = cr.tryToParseFromJSON(json);
    if(ret == true) {
        retval = cr;
        return retval;
    }

    if(ret == false) { 
        throw new JSONInvalidException("couldn't create model from JSON");
    }

    return retval;
}

解决方案

There's probably no problem with this. The compiler probably compiled the return retval inside the if statements into a jump that jumps to the return at the end of the function, which already has a return retval statement. The debugger showing the odd flow control doesn't mean that there is anything wrong.

这篇关于这是为什么Java的code执行多个return语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆