Primitives/Objects Declaration, default initialization values

寵の児 提交于 2019-12-17 09:58:34

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/13972617/primitives-objects-declaration-default-initialization-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!