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 声明,默认初始化值
基础教程推荐
- JPA惰性列表上的流 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 double 转换为 Int,向下舍入 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
