Java, What is the difference between assigning null to object and just declaration

后端 未结 2 1144
独厮守ぢ
独厮守ぢ 2020-12-03 02:48

What is difference between :

  • Object o = null; and
  • Object o; (just declaration)

Can anyone please answer me?

2条回答
  •  长情又很酷
    2020-12-03 03:13

    As mentioned, object reference as instance variable need not be assigned null as those take null as default value. But local variables must be initialized otherwise you will get compilation error saying The local variable s may not have been initialized.

    For more details you can refer this link

提交回复
热议问题