实例变量和局部变量的默认值 [英] Default values of instance variables and local variables

查看:55
本文介绍了实例变量和局部变量的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到Java为类属性提供了默认值,但没有为局部变量提供默认值.正确吗?

I read that Java provides default values to class properties, but not to local variables. Is that correct?

如果是这样,其背后的原因是什么?当您做得不错时,为什么不一直这样做呢?

If so, what is the reason behind this? When you are doing something good, why not do it all the way?

推荐答案

标准局部变量存储在堆栈中,并且在初始化之前不会实际创建.如果未使用局部变量,则它不会进入堆栈.但是,成员变量是在堆中分配的,因此具有默认的占位符(空引用或默认基元).

Standard local variables are stored on the stack and aren't actually created until they are initialized. If a local variable isn't used, it doesn't go on the stack. Member variables, however, are allocated in the heap, and thusly have a default placeholder (null reference or default primitive).

这篇关于实例变量和局部变量的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆