基元/对象声明,默认初始化值 [英] Primitives/Objects Declaration, default initialization values

查看:128
本文介绍了基元/对象声明,默认初始化值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明基元/对象时,它们是否被初始化?

When declaring primitives/objects, are them initialized?

哪个是默认值?

班级成员本地字段的行为是什么?

关于班级成员的对象声明怎么样?

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


请注意对象是初始化为 null

Please note that objects are initialized as null

推荐答案

默认值 int 0 ,这是它在JavaSE和JavaEE中的值,除非它被赋予了另一个值。

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.

您不能在Java(或任何其他原语)中拥有未初始化的 int 类成员。

You can't have an uninitialized int class member in Java (or any other primitive).

在您的示例中,您将显示 int 是一个类成员,在另一个例子中它是一个局部变量,这就是区别。

In your example you show the int is a class member, in the other example its a local variable, that is the difference.

对于类成员 JVM将设置默认值,对于局部变量,它会在您访问变量之前分配和初始值。

For class members JVM will put the default values, for a local variables it makes you assign and initial value before accessing the variable.

您可以查看默认值部分/nutsandbolts/datatypes.htmlrel =nofollow>原始数据类型,了解有关类成员默认值的更多信息。

You can check the Default Values section in Primitive Data Types for more information about the class members default values.

这篇关于基元/对象声明,默认初始化值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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