What's the difference between primitive and reference types?
From book OCA JAVA SE 7
Just as men and women are fundamentally different (according to John Gray, author of Men Are from Mars, Women Are from Venus), primitive variables and object reference variables differ from each other in multiple ways. The basic difference is that primitive variables store the actual values, whereas reference variables store the addresses of the objects they refer to. Let’s assume that a class Person is already defined. If you create an int variable a, and an object reference variable person, they will store their values in memory as shown in figure 2.13.
int a = 77;
Person person = new Person();
These are the primitive types in Java:
- boolean
- byte
- short
- char
- int
- long
- float
- double
All the other types are reference types: they reference objects.
This is the first part of the Java tutorial about the basics of the language.
Primitive Data Types :
- Predefined by the language and named by a keyword
- Total no = 8
boolean
char
byte
short
integer
long
float
double
Reference/Object Data Types :
- Created using defined constructors of the classes
- Used to access objects
- Default value of any reference variable is null
- Reference variable can be used to refer to any object of the declared type or any compatible type.