gpt4 book ai didi

java - 返回类型为 void 的方法应该使用 return 语句吗?

转载 作者:IT老高 更新时间:2023-10-28 21:03:39 26 4
gpt4 key购买 nike

我知道有时使用 return; 可以在 Java 中发挥有用的作用,例如保护:

public void foo(Bar bar) {
if(bar == null)
return;

// bar is not null, go ahead and do stuff with it
}

但是,如果只是到达返回类型为 void 的方法的末尾呢?例如,

public void printMenu() {
System.out.println("Print out some boilerplate info here, line 1.");
System.out.println("Print out some boilerplate info here, line 2.");
System.out.println("Print out some boilerplate info here, line 3.");

return;
}

除了纯粹的风格偏好之外,是否有任何理由支持或反对包含该 return;?如果有,它们是什么?

编辑:嗯,很快就得到了答复。总结下面发布的 15 个答案:“不。”

最佳答案

也许你是通过代码行获得报酬的?

除此之外,真的没有理由在最后放一个空的返回。

关于java - 返回类型为 void 的方法应该使用 return 语句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1909779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com