深入理解return语句
1.带有return关键字的java语句只要执行,所在的方法执行结束
2.在“同一个作用域”当中,return语句下面不能编写任何代码,因为
这些代码永远都执行不到。所以编译报错。
public class MethodTest08{
public static void main(String[] args){
System.out.println(m());
System.out.println(m1());
}
/* //编译报错:无返回语句
public static int m(){
int a = 10;
if(a > 3){
return 1;
}
} */
/* public static int m(){
int a = 10;
if(a > 3){
return 1;
}else{
return 0;
}
} */
public static int m(){
int a = 10;
if(a > 3){
System.out.println("Hello");
return 1;
//这里不能编写代码,编译错误,因为无法访问的语句
// System.out.println("Hello");
}
//这里的代码可以执行到
// System.out.println("Hello");
return 0;
//编译错误:无法访问的语句。
// System.out.println("Hello");
}
public static int m1(){
return 10 > 3 ? 1 : 0;
}
}