Java 方法深入解析:局部变量与返回值处理
1. 局部变量的深入探讨
1.1 局部变量的概念
局部变量是在方法内部声明的变量,方法外部的语句无法访问这些变量。不同的方法可以有相同名称的局部变量,因为各个方法无法看到其他方法的局部变量。
以下是一个示例代码:
public class LocalVars {
public static void main(String[] args) {
texas();
california();
}
public static void texas() {
int birds = 5000;
System.out.println("In texas there are " + birds + " birds.");
}
public static void california() {
int birds = 3500;
System.out.println("In california there are " + birds + " birds.");
}
}
程序输出:
In texas there are 5000 birds.
In california there are 3500 birds.
虽然有两个名为 birds 的变量,但程序一次只能看到其中一个,因为它们位
Java方法:局部变量与返回值解析
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



