Primitive Types and Reference Types in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends and welcome back in this lecture we will talk about primitive types and reference types in Java here is our outline we will talk about primitive types reference types and finally we will see the difference between primitive types and reference types so let's get started what are primitive types they are the types that hold simple values so for example byte short int long float double and the char are primitive types because they contain simple values so what's so special about primitive types have a look over here I'm defining a variable called I it is an integer and it is equal to 15 and also I have a character called C which is equal to the character a so if we want to imagine how these variables are stored in the memory it will be something like this we have a box that is called I and inside it we have the value 15 and also we have another box which is called C and then start it we have the character a and as we said before each box has an address so for example the address for the variable I is 300 and the address for variable C is 604 and of course I'm choosing the addresses randomly alright so what's important over here is that the variable contains the value now let's talk about reference types they are types that hold complex values which are objects okay so you already know that string type and you know that string is a class so string is a reference type right so now have a look over here I have a string called s and it is equal to this string over here so let's see how this string is stored inside the memory it will be something like this first of all we have a box that contains the string hello and this box has an address for example it is 1004 alright and we have another box which is called s and this is our variable right so first of all this box also have an address for example 800 what's interesting is the value of this box over here we have 1004 which is the address of hello right so when we are dealing with reference types the variable contains the address of the value as you see over here the variable s contains the address of the value hello right so this is why they are called reference the variable references the value you can imagine it like this we have the variable s and this variable points to the value hello this arrow is like a pointer or reference all right so this is one difference between primitive types and reference types now let's see some more differences between primitive types and reference types consider this code over here I'm defining two integers I 1 and I 2 I 1 is equal to 5 and I 2 is equal to I 1 all right and over here I'm defining two strings s 1 is equal to hello and s 2 is equal to s 1 so now we're going to see together what's happening inside our memory we will start with int I 1 is equal to 5 so we are going to create a new variable that is called I 1 and you will assign 5 to it so we can imagine it like this we have a box which is called I 1 and inside it we have the value 5 and of course this box has an address and this is random okay and this is because I 1 is a primitive type so the variable contains the value now let's talk about int I 2 equals I want so we are assigning the value of i1 to i2 right so this is an expression we will calculate the expression and then we will assign the value to i2 so we will create a new variable which is called I 2 and then we will assign the value of i1 to it so we can imagine it like this now we have a new variable called I 2 and inside it we have 5 because this expression over here is equal to 5 right it is equal to i1 and also I 2 has an address which is 200 and I will say it again I'm putting the addresses randomly so what's important over here is that I 1 and I 2 are different they are distinct variables and they contain distinct values this 5 over here is different than this 5 over here this 5 is the value of i1 and this 5 is the value of i2 the values of i1 and i2 are not related all right now let's see what happens when this statement is executed we are creating a variable of type string it is called as 1 and you are assigning it to hello so we are going to create a new variable as one and we will assign the address of hello to act right so to be something like this first of all we will store the hello in our box and this box has an address and we will create another box which is our variable s1 and the value of this variable is the address of hello which is 1008 right so s1 points to or references hello right and of course s1 is a variable so it also has an address which is 300 for example and this is because s1 is a reference type it is an object and specifically it is a string right so now let's consider string as 2 equals Aswan so we are going to create a new variable which is called s 2 and you will assign the value of Aswan to it right so over here this is an expression we will assign the value that is generated by this expression 2 s 2 right so it will be something like this we will create a variable called s 2 and this variable has an address which is 500 for example and the value of s 1 which is 1008 will be stored inside s 2 so we will have 1008 inside s 2 so now as you can see the address of hello is inside s 1 and also inside s 2 so s 1 and s 2 are different but they reference the same value as 1 is a variable and s 2 is a different variable but s 1 and s 2 reference the same value which is hello over here so we can summarize it like this when you are dealing with primitive types each variable is our box and inside this box we have the value so i1 is equal to 5 and i2 is equal to 5 this variable and the value is different than this variable and the value this 5 over here is different than this 5 over here but when we are dealing with reference types s 1 and s 2 will reference the same value and of course later we will see some more details about this but now I want you to get the idea so this is that thanks for watching and I'll see you in the next video [Applause] you [Music]
Info
Channel: Neso Academy
Views: 38,454
Rating: 4.9405942 out of 5
Keywords: primitive types, reference types, primitive types in java, reference types in java, java primitive types, java reference types, java, java lectures, java tutorials, java for beginners
Id: OmcFVHpb0v0
Channel Id: undefined
Length: 6min 27sec (387 seconds)
Published: Wed Jan 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.