What are Primitive Types?
A primitive data type is a type where the VALUE of the variable is stored to the variable name.
What are Reference Types?
A reference type is a type where a REFERENCE of the object is stored. ‘’okay, so what’s a REFERENCE? A REFERENCE is simply a location in the computer’s memory. Just think of it as an address.’’
What’s the difference?
Look at things this way… A primitive data type is like a basket of eggs. When you place 10 eggs into a basket, nothing much that you do external of the basket can affect the number of eggs in the basket. (Here’s a bunch of code that will probably scare the heck out of you if you feel that you’re not born to be a programmer. Here’s a piece of advice. Read the code, one line at a time. If you don’t understand it, read it again. Anything behind the “//” is friendly, I’m just trying to explain how you should think about each line of code. It really isn’t difficult, I promise.)
int x=10; //10 eggs int basket=x; //The VALUE of x is stored into the basket x=9; //x is changed, but the basket is remains the same. System.out.println(“x=”+x); //see! It’s 9! System.out.println(“basket=”+basket); //see! It still prints 10!
A reference data type is simply an address. When you use an object, the value isn’t stored, but instead an address of the object is stored. Take a home address for example, 16 Jefferson Park Avenue. It’s not possible for me to put a whole house into my basket, instead, I scribble the address down on a scrap of paper, “16 Jefferson Park Ave”. From now onwards, I whenever I want to do something to 16 Jefferson Park Ave, I simply reference the address. If I wanted to know the number of people in the house, I would need to go to the house, and count the number of people in there. This is necessary since all I have is the address, it would be impossible to determine information about the house just by looking at my own basket like before.
House h = new House(10); House jeff=h; h.setNumber(9); System.out.println(“h=”+h.getNumber()); System.out.println(“jeff=”+jeff.getNumber());
If you’re still stuck, try to think about the different results produced by the code. Try to resolve it within yourself why the results of both tests are different. It really is quite simple.
What do I need to know?
Now that you understand the concept (or at least I hope you do), you just need to know when something is a primitive data type, and when it’s a reference. Luckily for you, I have a complete list of primitive data types here. Anything that doesn’t fall into these data types is a reference data type. Here they are.
byte, short, int, long, float, double, boolean, char
Yes. This means String is a Reference Data Type! Okay that’s all I have for now, hope it was helpful!
Source Reference: smu.edu.sg/tsc/Reference_and_Primitive_types