user3175753
user3175753

Reputation: 13

Pointers in Java & memory

Suppose I define a new class, say PhoneBook. I do the following:

Phonebook x = new Phonebook()
Phonebook x1=x
Phonebook x2=x
...
Phonebook x99 =x

then this won't consume much memory will it since all the 100 variables are pointing to the same phonebook?

Thanks

Upvotes: 1

Views: 293

Answers (3)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136012

If those are local variables they do not consume any memory on the heap because they are on stack. The size of Java pointer is JVM implementation specific usually it is 32 bit.

Upvotes: 0

Rugal
Rugal

Reputation: 2717

Yes indeed you can also come to my blog Reference

What really consume memory is the first time you new a phoneBook, and the rest of Reference Phonebook consume just about 4byte each.

Upvotes: -1

Tim B
Tim B

Reputation: 41188

Correct. Each reference will consume a small amount of memory (usually 4 or 8 bytes on 32 or 64 bit systems) and that's it.

Upvotes: 5

Related Questions