java void函数中return语句有什么用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14000402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:42:19  来源:igfitidea点击:

What is the use of return statement in a void function

javaandroidreturn

提问by Blake

I m new to java, what does return;mean? is it like break?

我是java新手,什么return;意思?它像break吗?

  public void run() {
            if(imageViewReused(photoToLoad))
                return;
            Bitmap bmp=getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url, bmp);
            if(imageViewReused(photoToLoad))
                return;
            BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
            Activity a=(Activity)photoToLoad.imageView.getContext();
            a.runOnUiThread(bd);
        }

if the second imageViewReused(photoToLoad)returns true, BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad)won't be executed, right?

如果第二个imageViewReused(photoToLoad)返回true,BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad)则不会执行,对吗?

采纳答案by S.D.

Return statement skips the remaining execution of a function scope.

Return 语句跳过函数作用域的剩余执行。

Worth reading:

值得一读:

  1. return: http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
  2. break: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
  1. returnhttp: //docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
  2. breakhttp: //docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

回答by

Yes there is a similarity but there is also difference

是的,有相似之处,但也有不同

  • break- will stop a loop and switch condition. Can be used only for switch, and loop statements
  • return- will finish the function execution but the statements below of this keyword will not be executed. Can be used only for any functions.
  • break- 将停止循环并切换条件。只能用于 switch 和 loop 语句
  • return- 将完成函数执行,但不会执行该关键字下面的语句。只能用于任何功能。

Usage ofreturnkeyword in void function

void函数关键字的使用return

If you use returnin a void function like this

如果你return在这样的 void 函数中使用

void trySomething()
{
  Log.i("Try", "something");

  return;
  Log.e("Try", "something"); 
}

the execution of this function is done but the statement(s) below will not be executed.

这个函数的执行已经完成,但是下面的语句不会被执行。

Usage ofbreakkeyword

的使用break关键字

for any loop statements

对于任何循环语句

void tryLoop()
{
   while(true)
   {
      Log.d("Loop", "Spamming! Yeah!");
      break;
   }
}

the loop will be stopped and continue the remaining statements of this function

循环将停止并继续此函数的其余语句

for switch condition

切换条件

void trySwitch()
{ 
   int choice = 1;
   switch(choice)
   {
      case 0:
        Log.d("Choice", "is 0");
        break;
      case 1:
        Log.d("Choice", "is 1");
      case 2:
        Log.d("Choice", "is 2");
   }
}

using breakin switch condition is also same as loop. Omitting the breakwill continue the switch condition.

使用break在开关条件也是相同循环。省略break将继续切换条件。

回答by Evos

Yep, you can use it like a break.

是的,你可以像休息一样使用它。

回答by Niranj Patel

Yes, returnis break your next execuation of same block.

是的,return是中断您对同一块的下一次执行。

for more information about returncheck this

有关检查此的更多信息return

回答by arshajii

returnends the execution of the method in which it appears when it is called. For void methods, it simply exits the method body. For non-void methods, it actually returnsa value (i.e. return X). Just be careful with try-finally: remember that the finallyblockwill be executed even if you returnin the tryblock:

return结束调用它时出现的方法的执行。对于 void 方法,它只是简单地退出方法体。对于非空方法,它实际上返回一个值(即return X)。请注意try-finally:请记住,即使您在块中,finally也会被执行:returntry

public static void foo() {
    try {
        return;
    } finally {
        System.out.println("foo");
    }
}

// run foo in main
foo

Thisis a good reference for learning more about return.

是进一步了解return.



is it like break?

它像break吗?

Well in the sense that both statements 'end' a running process; returnends a method and breakends a loop. Nevertheless, it is important to know the differences between the two and when each should be used.

好吧,这两个语句都“结束”了一个正在运行的进程;return结束一个方法并break结束一个循环。尽管如此,了解两者之间的差异以及何时应使用两者之间的差异非常重要。

if the second imageViewReused(photoToLoad)returns true, BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad)won't be executed, right?

如果第二个imageViewReused(photoToLoad)返回trueBitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad)将不会被执行,对吗?

Correct - the method will "return" if the body of that if-statement is executed and no subsequent statements will be reached.

正确 -return如果if执行该语句的主体并且不会到达后续语句,则该方法将“ ” 。

回答by viSH98

Here return act as end of function. You can avoid it by changing your code as,

这里 return 作为函数的结束。您可以通过更改代码来避免它,

 public void run() {
        if(!imageViewReused(photoToLoad))
        {
          Bitmap bmp=getBitmap(photoToLoad.url);
          memoryCache.put(photoToLoad.url, bmp);
          if(!imageViewReused(photoToLoad))
          {
            BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
            Activity a=(Activity)photoToLoad.imageView.getContext();
            a.runOnUiThread(bd);
          }
    }

回答by Sahil Mahajan Mj

A function's execution is finished, when it comes to the return statement and then it returns back to its invoking code. In your case,

当涉及到 return 语句时,函数的执行完成,然后返回到其调用代码。在你的情况下,

if imageViewReused(photoToLoad)is true, then the code block after returnwill not get executed.

如果imageViewReused(photoToLoad)为真,则后面的代码块return将不会被执行。