Primitives/Objects Declaration, default initialization values

前端 未结 1 1280
名媛妹妹
名媛妹妹 2020-12-02 01:17

When declaring primitives/objects, are them initialized?

Which are the default value?

What is the behavior on class members and local fields

相关标签:
1条回答
  • 2020-12-02 01:29

    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.

    0 讨论(0)
提交回复
热议问题