1

What happened reference types and primitive types in Java in real.

Many Java manuals explain about difference between reference type and primitive type. Those manuals say variables of primitive type store store actual values in directly. Variables of reference type store the address of memory.

This is my limited knowledge of computer. data is number of binary number about switch of transistor. memory can store such numbers.

Memory of computer store such number. Then, what is variable?

complement of the question: I think that explanation of primitive and reference type of above sentences are a result of simplifying. What are things behind the result?

tanaka
  • 21
  • 2
  • 1
    Please only ask one question per post. You can [edit] your post. – Sweeper Jun 21 '20 at 06:39
  • 1
    Does this answer your question? [What's the difference between primitive and reference types?](https://stackoverflow.com/questions/8790809/whats-the-difference-between-primitive-and-reference-types) – Nowhere Man Jun 21 '20 at 06:47

2 Answers2

2

Primitive variables store the actual values, whereas reference variables store the addresses of the objects they refer to.

Primitive types are predefined by the java language :

  1. boolean
  2. byte
  3. short
  4. char
  5. int
  6. long
  7. float
  8. double

All the other types are reference types and they are meant to reference objects.

Reference/Object Data Types are usually created using defined constructors of the classes and can be used to access objects, refer to any object of the declared type or any compatible type.

Read more at : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/

Amit kumar
  • 2,169
  • 10
  • 25
  • 36
  • Thank you for answer. My question was requested to simplify. I could edit my question clearly from your answer's sentence. This is my blush-upped question:) Thank you very much. – tanaka Jun 27 '20 at 15:19
2

This confusion is very common and I don't even blame you for it. Well, let us break it down. The contents of a primitive type are simply their associated values. For instance, if I say:

int x = 10;

Here, x is allocated 4 bytes of memory and the value 10 is directly stored in that memory location. You'll see all primitive type names are all lowercase in Java (int, char, double and so on)

On the other hand, with reference types, the variable actually does not contain the value associated with it. In Java, Strings, Arrays and all library classes are reference types . Moreover, all user-defined types you create using classes are also reference types. How are they different? Let's look at arrays. If I say:

 int[] array = new int[10];

Here, new int[10] will allocate memory for 10 integers and it will not directly be stored in the variable array. After allocating the memory, array is assigned a value that is the memory location just allocated. That is array does not actually contain the array of 10 integers but it contains the memory address/reference to that another memory location actually containing the 10 arrays. Let's see in action how they work differently: I'm creating two integers here:

int n1 = 5;
int n2 = n1;
n2 = n2 + 2;

You can see, first n1 is assigned as 5, then when I assign n2, the value 5 is simply copied from n1 to n2. When I do n2 = n2 + 2;, the value of n2 becomes 7, but n1 does not change, it stays 5.

Now, about reference types:

int[] a1 = {1, 2, 3};
int[] a2 = a1;
a2[0] = a2[0] + 5;

I first create an array a1, then I create a2 and assign it as a1. Here's the fun part, when I assign a2 to a1, the entire array is not copied to a2, the memory location/reference pointing to that another memory location that contains the array is copied. You can see, I add 5 to a2[0] that is, the first element 1. Now, a2[0] becomes 6. Not just that, a1[0] also becomes 6! This is because both variables a1 and a2 are referring/pointing to the same memory location!

Hope this helps. If you have an more queries, let me know :)

Amal K
  • 4,359
  • 2
  • 22
  • 44
  • Thank you for answer. I brush upped answer because my question was requested to simplify. If you can see, see my question. Thank you very much. – tanaka Jun 27 '20 at 15:21