Primitives/Objects Declaration, default initialization values(Primitives/Objects 声明,默认初始化值)
问题描述
在声明原语/对象时,它们是否被初始化?
哪些是默认值?
类成员和本地字段的行为是什么?
类成员上的对象声明呢?
<小时>如下所述,这些是默认值:
<块引用>数据类型 - 默认值(用于字段)字节 0短 0整数 0长0L浮动 0.0f双0.0d字符 'u0000'字符串(或任何对象) null布尔假
请注意,对象被初始化为null
int
的默认值是 0
,它在 JavaSE 和JavaEE,除非它被分配了另一个值.
Java(或任何其他原语)中不能有未初始化的 int
类成员.
在您的示例中,您显示 int
是一个类成员,在另一个示例中它是一个局部变量,这就是区别.
对于 class members
JVM 会放置默认值,对于 local variables
它让你在访问变量之前分配和初始值.
您可以查看 Default Values 部分>原始数据类型了解有关类成员默认值的更多信息.
When declaring primitives/objects, are them initialized?
Which are the default value?
What is the behavior on class members and local fields?
What about objects declaration on class members?
As answered below, these are the default values:
Data Type - Default Value (for fields) byte 0 short 0 int 0 long 0L float 0.0f double 0.0d char 'u0000' String (or any object) null boolean false
Please note that objects are initialized as null
The default value of int
is 0
and that is the value it will have in both JavaSE and JavaEE unless it was assigned with another value.
You can't have an uninitialized int
class member in Java (or any other primitive).
In your example you show the int
is a class member, in the other example its a local variable, that is the difference.
For class members
JVM will put the default values, for a local variables
it makes you assign and initial value before accessing the variable.
You can check the Default Values
section in Primitive Data Types for more information about the class members default values.
这篇关于Primitives/Objects 声明,默认初始化值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Primitives/Objects 声明,默认初始化值
基础教程推荐
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01