Do uninitialized primitive instance variables use memory?

前端 未结 6 1909
庸人自扰
庸人自扰 2020-12-14 14:37

In Java, does it cost memory to declare a class level instance variable without initializing it?
For example: Does int i; use any memory if I don\'t initial

6条回答
  •  太阳男子
    2020-12-14 15:06

    In Java, when you declare a class attribute such as String str;, you are declaring a reference to an object, but it is not pointing yet to any object unless you affect a value to it str=value;. But as you may guess, the reference, even without pointing to a memory place, consumes itself some memory.

提交回复
热议问题