且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java中的局部变量和实例变量有什么区别?

更新时间:2022-05-08 03:29:20

我能想到的另外一件事:

One extra thing I can think of:

实例变量具有默认值,即,如果是对象引用,则为null;如果是int,则为0.

Instance variables are given default values, i.e., null if it's an object reference, and 0 if it's an int.

局部变量不会获取默认值,因此需要进行显式初始化(​​并且如果操作失败,编译器通常会抱怨).

Local variables don't get default values, and therefore need to be explicitly initialized (and the compiler usually complains if you fail to do this).