Java defining or initializing attributes of a class

前端 未结 3 1638
生来不讨喜
生来不讨喜 2021-02-05 06:35

Is there a difference between defining class attributes and initializing them? Are there cases where you want to do one over the other?

Exampl

3条回答
  •  忘了有多久
    2021-02-05 07:17

    In Java, initializing is defined explicitly in the language specification. For fields and array components, when items are created, they are automatically set to the following default values by the system:

    numbers: 0 or 0.0

    booleans: false

    object references: null

    This means that explicitly setting fields to 0, false, or null (as the case may be) is unnecessary and redundant.

提交回复
热议问题