What Is The Difference Between a Primitive and Reference Type in Java? | For Beginners

preview_player
Показать описание
Primtive types and references types are two different variable types in Java. As a beginner Java programmer, it's important to know the difference between the two. This video will expertly explain the difference between the two.

Subscribe If You Enjoyed :)

#codingforbeginners #programmingbasics #java #shorts
Рекомендации по теме
Комментарии
Автор

Primative or value types always have a fixed size.
For example, a float will always be 4 bytes or 32 bits. And a double is twice an float type.
All primative(value) types directly contain their data on the stack.
That means that primative or value types contain the actual data
at the same memory location as where the variable is stored.
All primative types use one memory location which is on the stack.
A Java example int x = 5 ;
The value of 5 is stored at the same place the variable "x" is stored.
Also primatives or value types do not have methods.
Reference types do not hold the actual data. They only tell the computer where to find the actual data.
When you create reference variables. Two memory locations are created, One for the object itself, where the actual data is stored, which is on the heap. And an reference variable is created, which is stored on the stack.
All objects don't have a name. Objects are anonymous. Only reference variables have names.
In Kotlin code we have this
code var x :Int =5
An Int object with an value of 5
is created on the heap. It has no
name, it is anonymous . Also a new variable(reference) is created on the stack and it has a name called "x" which holds the reference to that object
Now let's look at a Java example ,
String jan ="Jan";
An String object is created on the heap, with a text value of "Jan"
And an string reference variable
called "jan", is created on the stack, which holds the reference
(memory address) to that string object.
This also applies to Python
x = 3
An int object with an value of 3
is created on the heap. This int object is anonymous.
And the reference to that int object is assigned to the reference variable called "x" on the stack.

rickzalman