Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between primitive and reference types?

This is a past exam question and I was wondering what a primitive type and reference type are first off? With an array I know the a reference type is where the array is composed of objects or variables, but a primitive type is where you would create the array with just int or strings. (right?)

How do you think you would answer the question on the test and be given good credit? Without really referring directly to an primitive ARRAY type... Is there a way to do it without that? Or do you think it would be fine to just explain it with the array.

like image 413
WestJackson Avatar asked Jan 09 '12 15:01

WestJackson


People also ask

What is the difference between a primitive value and a reference value?

Primitive values are data that are stored on the stack. Primitive value is stored directly in the location that the variable accesses. Reference values are objects that are stored in the heap. Reference value stored in the variable location is a pointer to a location in memory where the object is stored.

What are two differences between primitive types and class types?

int , float , double , long , short , boolean and char are examples of primitive data types. You can't invoke methods on these data types and they don't have a high memory footprint, which is their striking difference from classes. Everything else is a class (or class-like in the case of interfaces and enums).


1 Answers

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(); 

enter image description here

like image 104
BERGUIGA Mohamed Amine Avatar answered Oct 23 '22 13:10

BERGUIGA Mohamed Amine