Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

What is the difference between Primitive and Reference type variables? What makes a variable Primitive? Java

Hi,

I understand which types are Primitive but I don't understand what makes them Primitive, I've read a book and several websites on it but I'm still confused.

Could someone help explain to me what Primitive means and what makes a variable Primitive.

Thanks in advance!

Chris

1 Answer

Hi Chris - Primitive variables store the actual values, meaning that if you go to the variable's memory location you will find the value of the variable. However, reference variables store the addresses of the objects they refer to, so if you visit their memory locations you find the bits representing a way to get to an object on the heap, which is an area of memory where objects live.

Thank you so much, I've read text books, online and asked friends and this is the best wording. Huge thank you! Can you please explain why a String isn't a Primitive variable type?

A comment from Yin Zhu in THIS stackoverflow post says:

"int, char, float, double, etc. all have a fixed length in memory. e.g. a int have 4 bytes, thus 32bits.

but a string can have different length, it is actually an array of char"

I think it answers your second question a little bit :)

Thanks Grigorij, I appreciate the help too!

:thumbsup: