Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do uninitialized primitive instance variables use memory?

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 initialize it with i = 5;?

Details:

I have a huge super-class that many different (not different enough to have their own super classes) sub-classes extend. Some sub-classes don't use every single primitive declared by the super-class. Can I simply keep such primitives as uninitialized and only initialize them in necessary sub-classes to save memory?

like image 918
WVrock Avatar asked Oct 27 '14 10:10

WVrock


People also ask

What happens in Java if you try to use an uninitialized variable?

Accessing an uninitialized local variable will result in a compile-time error. Show activity on this post. The default value will be based on the type of the data and place where you are using initialized variable .

Can instance variables be used without initialization?

If instance variables are declared only and not initialized, they will be assigned default values by JVM before constructor execution. If instance variables are declared with initialization, then these lines will be moved within every constructor of my class. And thus, execution will be done in constructor by compiler.

In which memory are instance variables stored?

Heap is a memory place where the objects and its instance variable are stored.

What is an uninitialized variable set to?

In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. As such, it is a programming error and a common source of bugs in software.


2 Answers

All members defined in your classes have default values, even if you don't initialize them explicitly, so they do use memory.

For example, every int will be initialized by default to 0, and will occupy 4 bytes.

For class members :

int i; 

is the same as :

int i = 0; 

Here's what the JLS says about instance variables :

If a class T has a field a that is an instance variable, then a new instance variable a is created and initialized to a default value (§4.12.5) as part of each newly created object of class T or of any class that is a subclass of T (§8.1.4). The instance variable effectively ceases to exist when the object of which it is a field is no longer referenced, after any necessary finalization of the object (§12.6) has been completed.

like image 138
Eran Avatar answered Sep 27 '22 19:09

Eran


Yes, memory allocates though you are not assigning any value to it.

int i; 

That takes 32 bit memory (allocation). No matter you are using it or not.

Some sub-classes don't use every single primitive declared by the super-Class. Can I simply keep such primitives as uninitialized and only initialize them in necessary sub-classes to save memory?

Again, no matter where you initialized, the memory allocates.

Only thing you need to take care is, just find the unused primitives and remove them.

Edit: Adding one more point that unlike primitive's references by default value is null, which carries a a memory of

 4 bytes(32-bit)   8 bytes on (64-bit) 
like image 37
Suresh Atta Avatar answered Sep 27 '22 20:09

Suresh Atta