简体   繁体   English

为什么在所有if-else分支都具有return语句的情况下,方法中的if-else if-else(在循环内)给出“ return语句丢失”的信息?

[英]Why if-else if-else (within a loop) in method gives “return statement missing” while all if-else branches have a return statement?

Why compiler gives me "return statement missing" erorr? 为什么编译器给我“ return statement missing”错误?

All possible conditional branches (if-else) contain a return statement. 所有可能的条件分支(if-else)都包含一个return语句。

But if I have the same if-else-if-else construct alone (not inside for() loop) - it compiles fine, without producing this "return statement missing" erorr? 但是,如果我单独拥有相同的if-else-if-else构造(不在for()循环内)-它可以很好地编译,而不会产生此“ return statement missing” erorr吗?

public Resume get(String uuid) {

        // compiler shall know that array is not empty!
        final Resume[] storage = new Resume[100]; // same for static final

        // is executed at least once - see above!
        for(Resume resume : storage) {
            if (resume == null) {
                return null;                
            } else if (resume.uuid.equals(uuid)){
                return resume;
            }  else {
                return null;
            }            
        }
        // return statement is missing !!!      
    }

I could understand the compiler - it is afraid that the loop would never execute if the array named storage is empty. 我能理解编译器-担心如果名为storage的数组为空,则循环将永远不会执行。 Why compiler ignores the size of the array? 为什么编译器会忽略数组的大小? But it is just a half of problem with advanced for loop. 但这只是高级for循环问题的一半。

Let's take a regular for loop, totally independant of storage array size. 让我们进行一个常规的for循环,它完全独立于存储阵列的大小。

public Resume get(String uuid) {
        Resume resume = new Resume();

        // being a static final field produces same compile error...
        final int LIMIT = 100;

        // LIMIT is known to compiler, since it is final primitive.
        // for loop execution not dependent on storage array being empty
        for (int i = 0; i < LIMIT; i++) {       
            if (resume == null) {
                return null;                
            } else if (resume.uuid.equals(uuid)){
                return resume;
            }  else {
                return null;
            }            
        }
        // return null;   //statement is missing !!!; //       
    }

What explains this second for loop problem? 是什么解释了第二个for循环问题?

PS I think it is because things like "i < LIMIT" (even LIMIT being final) do not evaluate to true during compilation, true appears only at runtime , so at compile-time compiler is afraid just-in-case ;) PS我认为这是因为诸如“ i <LIMIT”(即使LIMIT是最终的)之类的东西在编译期间不会评估为true,true仅在运行时出现 ,因此在编译 ,编译器担心只是在这种情况下;)

Even though you assign a non empty array to the storage variable, the compiler doesn't take that into account when considering whether all execution paths have a return statement. 即使您为storage变量分配了一个非空数组,编译器在考虑所有执行路径是否都具有return语句时也没有考虑到这一点。

As far as the compiler is concerned, it's possible that the for loop will have 0 iterations, and therefore there must be a return statement after the loop. 就编译器而言,for循环可能有0次迭代,因此循环后必须有一个return语句。

In fact, even if you use only constants in the loop's condition, the compiler will still require a return statement after the loop. 实际上,即使在循环条件中仅使用常量,编译器在循环之后仍将需要return语句。 The following method won't pass compilation: 以下方法不会通过编译:

public static int test() 
{
    for (int i = 0; i < 5; i++) {
        return 3;
    }
}

What if your storage is empty, you will never go into loop, thus you wouldn't reach any of your return statements. 如果您的storage为空,您将永远不会进入循环,因此您将无法获得任何return语句。

At compile time you couldn't guarantee that your collections would never be empty, thus compiler need a return statement for this branch of execution. 在编译时,您不能保证您的集合永远不会为空,因此编译器需要此执行分支的return语句。

如果storage为空,则不会发生退货。

所有可能的条件分支都在for循环中,如果您的存储数组为空而不是return语句,该怎么办呢,这就是编译器给您此错误的原因

Change your code like this because if storage is empty, it pass the for : 像这样更改代码,因为如果storage为空,则传递for

public Resume get(String uuid) {

        // compiler shall know that array is not empty!
        final Resume[] storage = new Resume[100]; // same for static final

        // is executed at least once - see above!
        for(Resume resume : storage) {
            if (resume == null) {
                return null;                
            } else if (resume.uuid.equals(uuid)){
                return resume;
            }  else {
                return null;
            }            
        }
        return null;      
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM