Java中的局部变量和实例变量有什么区别? [英] What is the difference between local and instance variables in Java?
本文介绍了Java中的局部变量和实例变量有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
除了范围和存储方面的区别外,Java中实例变量和局部变量之间是否还有其他主要区别?
Except the scope and the storage differences, are there any other major difference between instance and local variables in Java?
推荐答案
我能想到的另外一件事:
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).
这篇关于Java中的局部变量和实例变量有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文