Sourav, Just think about a normal Object creation. class Sourav { } Sourav sourav = new Sourav(); here the object “sourav” is going to be stored in heap memory.why? because the object creation is a runtime operation and all the runtime stuff gets stored in the heap memory. just think like if you are using the keyword “new”, then that's a run time operation. case 2: in the above case, Sourav is a normal class. But what about if I create an object for the String class. String sourav = new String(“Sourav Sarkar”); here the above object “sourav”is a little special. Why ? because it’s a String class object and you might have already figured it out that something tricky is going on here.right? but it’s very simple. so first let’s apply our logic.sourav is a what? it’s an object. if it’s an object where it is going to be stored? of course, the answer is, in the heap memory. right?. So as of now, one object is already created inside the heap. now the next thing, I told you the string class is a little special. and the object “sourav” is a String class object. So to store the string objects, we also have something called, String constant pool(SCP) which exits inside the Heap memory only. Imagine like, you have a box(heap) and inside that box, you have another small box(SCP) which will store all the constants. In the above case, first the object got created in heap because crating object is a runtime operation, and then “Sourav Sarkar " is a constant too, so another object got created inside the SCP(the job of scp is to store constants). So whenever we use the new keyword to create a string object, two objects get created, one in the heap and one in the scp. But what about String sourav = “Sourav Sarkar”; //it’s an another way to create a String object here tell me, are we using the new keyword? nope, right? so if we are not using any new keyword it’s not a runtime operation, so no chances of object creation in the heap. but wait, “Sourav Sarkar “ is a constant here and where we store the String constants ? in the String constant pool(SCP) right? so an object will be created inside the SCP. So, in this case, only one object will be created and that will be inside the SCP. now here is a task for you. String str = new String(“abhilash”); str.concate(“Panigrahi”); let me know how many objects will be created in the memory? A clue for you is concate(“panigrahi”) is a runtime operation.it’s tricky but just apply the logic.you can surely do it.
@@SeleniumExpress Case 1 : String str = new String(“abhilash”); here this is a run-time opertion because we are creating String object using new keyword. So one object will be created in the heap. But here "abhilash" is a constant, so another object will be created in string constant pool. So, here two objects will be created for case 1. Case 2 : str.concate(“Panigrahi”); here concate is runtime operation so one object will be created in heap. but "Panigrahi" is a constant too, so another object will be created in the string constant pool. So, here two objects will be created. So the conclusion is that, total four objects will be created.
@@souravsarkar1615 not four objects it is only three ibjects because already heap had the earlier one oject so it is just point to another object it will not create another object of new keyword
I Leared today on .Intern(), By applying String. intern() on a couple of strings will ensure that all strings having the same contents share the same memory. clear cut explanation! Highly appreciated!
By new keyword : Java String is created by using a keyword “new”. For example: String s=new String(“Welcome”); It creates two objects (in String pool and in heap) and one reference variable where the variable 's' will refer to the object in the heap.
All your contents are really good with explanation on how things internally work and that's what makes it a class apart. I see few people's comment to correct your accent , but trust me you don't have to if you want followers from other country. I'm working in an investment banking company and i see lot of Indians who are settled in US speak the same way so that others get their accent as they (Americans or people from Europe) don't get our accent that easily.
Hmm... I have a different thought on this. This video explains that strings are immutable because of the string pool. However, I'm going to say that it's the opposite - String pool essentially works due to the immutable nature of strings. So, what's the actual reason for the immutable nature of strings? Here's a theory - A string is stored internally as an array of bytes. 1. What happens when a string is modified? The byte array will most likely need to be resized. 2. What if the array cannot be resized because the subsequent blocks of memory are already occupied? A new byte array needs to be created with a sufficient size, all bytes from the current string copied over to the new array, free up the old array eventually (GC). Now, #2 is going to happen in almost every scenario. So, that's immutability occurring automatically by itself. Since that already happens, it probably makes sense to make it behave immutably for every string because it's probably more efficient that way, rather than handling every specific case. Now because strings can behave as immutables almost automatically, a string pool helps in keeping a check on memory to a certain degree. Even the concept of using strings as hashes is due the same reason. So, string pool and string hashes are a result of the immutable nature of strings and NOT the other way round.
Dear instructor, I have two questions, 1. What is String pull in general? is it an object in heap memory yes? 2. Nevertheless the comparison of String s = ""ABC" vs String s = new String("ABC") is already discussed, but in one tutorial instructor mentions that even when we create String in String s = new String("ABC") way the object is created in heap but that object points to the "ABC" value in string pool, is this what happens behind the scenes? Or when we do new then a String object is created only in heap which holds ABC value? So if string pull is empty and we create new String("ABC") object then it will be created in the heap and string poll will still stay empty? Thanks
Avi you are doing a great job. You are very clear with the point that you explain. Just one suggestion if you can consolidate all the things together in one real time project. Very few person come with such an clear explanation.
Hi Rajnish, Thanks for the suggestion. I will be having an another video on my channel where I will cover how immutability of string help us in a multi threading environment and in web world. I will try to come up with some real-time stuff.
Hey Thanks a lot Avi. Your style of explaining the concepts is really very good. You go in depth to explain the concept. Really appreciate your efforts. Request you to start a series on Collection Framework and also on Java 8.
Hi Avi You have clearly explained the concept. great job. I 'have also gone through all the comments below.. You explained all the queries with care. Nice will surely browse through your videos.
please can you do the Multi-Threding videos in Java. I am facing this issue so many times in Interview But I couldn't clear this type of quation in Interview
@Selenium Express have some query related to String immutable public class Main { public static void main(String[] args) { String s="Selenium Express"; String s1="Selenium Express"; s=s.concat("RUclips"); } } in this case after i concat "RUclips" to s and assign it to reference s new object will be created in Heap and also in String Constant Pool ,will s ="Selenium Express" is still present in SCP or s reference will be removed ? . Isn't we are modify same string s Thanks
In the following example I am getting the output as "References are unequal' but i am expecting the output as "References are equal" since S3 and S4 are created using string class. Can you explain me this String S1="Mary"; String S2="John"; String S3=S1+S2; String S4=S1+S2; if (S3==S4) SOP("References are equal"); else SOP("References are unequal");
Sir, how does garbage collector works in string constant pool? Since you said that string objects are cached in SCP, there should be some provisions to delete the objects present in SCP which are no longer in use or else we will eventually run out of memory.
Can you please explain how the memory is decided or checked inside SCP or heap weather the object is present or not? Eg String str=new String("avi"); it will create object in both heap and SCP? and String s1= "avi"; will point to same only?
Hi Vinay, You right on your point. Using " " you can create a string on the pool directly which is memory efficient. But using new keyword or by using constructor you too have an option to create an string object. The time you are going to need it, think about an situation when you need to convert an Char array to a string, you might need an constructor of string class which accept a char array, to convert that array to string. Eg char charArray[] = {'a','b','h','i'}; String convertingCharArray= new String(charArray) System.out.println(convertingCharArray); //abhi Like that , we can get few different constructor of String class which is used to construct a string by passing different argument. Please refer the below url which will redirect you to the string api and find the constructor section . docs.oracle.com/javase/7/docs/api/
Hey Avi i have one question. Why our java file name must be the same as public class name ? We can take any number of classes in a java file why only one public class is allowed and whose name should be class name ? i hope u will answer my question, coz i have been asked this question in many interviews but im not getting clarification any where else.
as per my understanding only 2 because first line creates AB in heap as well as scp, second line will not create AB in scp because AB is already there in scp so s2 will just point to already existing AB . simple
public class Main { public static void main(String[] args) { System.out.println("Thanks Selenium for the tut."); String first="Testing SCP", second= new String("Testing SCP"), third= new String("Testing SCP").intern(), fourth= "Testing SCP"; System.out.println(fourth==first); //true System.out.println(fourth==second); //false System.out.println(fourth==third); //true } }
Hey Avi. Good description of the concepts. I wanted to know the use of intern method. What;s the use of creating an instance with new keyword if we pointed it to scp using intern. We could have directly created the string using literals right?
Hi Manmeet, You right on your point. Using " " you can create a string on the pool directly which is memory efficient. But using new keyword or by using constructor you too have an option to create an string object. The time you are going to need it, think about an situation when you need to convert an Char array to a string, you might need an constructor of string class which accept a char array, to convert that array to string. Eg char charArray[] = {'a','b','h','i'}; String convertingCharArray= new String(charArray) System.out.println(convertingCharArray); //abhi Like that , we can get few different constructor of String class which is used to construct a string by passing different argument. Please refer the below url which will redirect you to the string api and find the constructor section . docs.oracle.com/javase/7/docs/api/ Feel free to post an another question if you have one.
Hi.. please explain.. below mentioned code.. I have little confusion.. String a="hi"; a="hello"; In this situation string a value to be changed.. so how is it possible? Please solve this asap.. thank you
Hi Avilash ! According to this video. I like this explanation, but I have small doubt please do clarify. As we can see that you saying because of "String Constant Pool" the "String is Immutable". Is this correct because I heard that "String Constant Pool" is introduced after "String Immutable" introduction. So how can you tell that because "String Constant Pool" so on so process "String is Immutable". Please do clarify on this point. And I assume that String is Immutable because we use the String value as an key in hashmap which is popular !!!
You are correct, string is immutable for many reasons. As you said its used as a key in hashmap. It's too, popular for hashcode caching as well. String immutable nature also helps us to use it for security purpose. For an example, think that we are using it as parameters in DriverManager.getConnection().all parameters inside that methods like url, username, password are Strings. Even if a hacker wants to change it ,he will end up creating a new object as string is immutable. String is immutable, so by default it's thread safe. it has many reasons like this.you can verify it in Java docs.
How is the equality and hashcode methods overridden in strings to differentiate between strings created as literals and those created using new keyword ?
Hi Avi, regarding intern() method, if s1 points to the reference in SCP, then a new object will still be created in Heap and allocates a memory for it with the same content ?
Hi Mohan. Yes, If we will create another string object with the same content again(using the new keyword), then it is going to create a different object in the heap. To point that object to SCP, again we need to use intern method. Please refer the below code. String str = "Avi"; String str1 = new String("Avi").intern(); System.out.println(str == str1);//true String str2 = new String("Avi"); System.out.println(str==str2);// false Please Keep me posted, If this answer doesn't help.
@@SeleniumExpress Hi Avilash, quick question, so why would we want to do String str1 = new String("Avi").intern() when the reference is going to be pointing to the string literal in SCP and not the object created in heap? Wouldn't this object get garbage collected since there is nothing pointing to it. P.s. Thank you for your videos, you are so amazing!
Dear I have question 1. String s = 'avi'; String s1=new ('avi'); sys(s.hashcode()); Sys(s1.hashcode()); Output -> -1200,1200 You say s1 is created in heap memory and s is created in SCP then how we get false if both are pointing to same memory location 2. If I use intern() then I get true I am not understanding this scenario ..plz respond me
Hi Aniket, Thank you for posting a comment. So when you create 's1' object, you used the new keyword.As object creation is done at runtime, the s1 object goes to heap area. and 's1' is pointing to the object which is there in heap area. when you create 's' object, you have use double quote or " " to create the object.When you create an object like this(using " "), it's called a constant, so it goes to SCP. so in this case, an object will create in SCP and 's' is pointing to that object. So in the above scenario, two different objects got created one in heap and one in SCP respectively. so these are the different object. 's1' and 's' are not pointing to the same object.That's why you got false if you compare them (s1==s2) .(double equals (==) checks reference) Don't be confused with the hashcode.hashcode is coming same because both the content is same.but what matters is how you are creating the object.Is it by using new keyword or by using " " . Question 2 : when you interning an object by using the intern method the reference is moving to SCP. for an example if you create an object like below String s = "aniket"; then an Object will be created in SCP (as you have used "" to create the object) and s will be pointing to the object in SCP. And again when you are trying to create an object by using a new keyword like below : String s1 = new String("aniket"); an object is getting created in the heap as you are using new keyword and no other object is going to be created in SCP as 'aniket' is already present there. and as I described you already, if you are using new keyword the reference is going to point to heap. but if you want to move the reference to SCP or if you want to use the object 'aniket', which is there in SCP then you have to use intern(). for an example : String s1 = new String("aniket").intern(); now the reference will move to SCP . so both 's' and 's1' object pointing to the same object in the SCP and you get 'true' as a result if you compare them (s==s1") let me know if you don't get me.you can refer the below video for string object creation. ruclips.net/video/xldyBBdpknM/видео.html keep me posted if you have any further queries. Have a nice time Aniket!!
Hi, can you suggest what will happen with below code? String str="avi"; str = str.concat("lash"); In this string allowing me to mute the object, may i know the reason?
@seleniumexpress Are you sure that String s = new String(“Avi”); automatically creates string object in SCP? I dont think this is true because then the concept of “Interning” string objects of heap into SCP would then be unnecessary. Kindly respond.
Hi can you please upload a video for spring, maven and junit test integration with this web app using some use case with database, ex : online movie booking and cancellation
I have a small question related to intern() API. As you said that this API forces the JVM to point the address available in SCP even though we use the new keyword. Can you please help me to understand, String s= new String("Rajnish").intern(); Is that the memory created in heap area is ready for GB collection.
Rajnish Sharma Yes absolutely, the object which was created in heap will be no more in use as u have used intern(). So it will be ready for garbage collection
"Lash" will be created in the scp are as it is in literals.but avilash will be created in heap are as we are using concat() and it is a runtime operation only if it has a reference like...... String s2=str.concat("lash").
hi.. great content, but as you said a new object is being created in the heap section while using new String("Avi"). It means now the reference is not to the SCP so we should be able to mutate the string obj in that case. ??
so why when i call hashCode on String s = new String("Abc") and Strin s2 = "Abc" hashcode and equals is true ? its same object, so ... there is not s on heap
String s = ""ABC" vs String s = new String("ABC"); Can u explain this?
Sourav,
Just think about a normal Object creation.
class Sourav
{
}
Sourav sourav = new Sourav();
here the object “sourav” is going to be stored in heap memory.why?
because the object creation is a runtime operation and all the runtime stuff gets stored in the heap memory. just think like if you are using the keyword “new”, then that's a run time operation.
case 2:
in the above case, Sourav is a normal class. But what about if I create an object for the String class.
String sourav = new String(“Sourav Sarkar”);
here the above object “sourav”is a little special. Why ? because it’s a String class object and you might have already figured it out that something tricky is going on here.right?
but it’s very simple.
so first let’s apply our logic.sourav is a what? it’s an object. if it’s an object where it is going to be stored? of course, the answer is, in the heap memory. right?. So as of now, one object is already created inside the heap.
now the next thing, I told you the string class is a little special. and the object “sourav” is a String class object.
So to store the string objects, we also have something called, String constant pool(SCP) which exits inside the Heap memory only. Imagine like, you have a box(heap) and inside that box, you have another small box(SCP) which will store all the constants. In the above case, first the object got created in heap because crating object is a runtime operation, and then “Sourav Sarkar
" is a constant too, so another object got created inside the SCP(the job of scp is to store constants). So whenever we use the new keyword to create a string object, two objects get created, one in the heap and one in the scp.
But what about
String sourav = “Sourav Sarkar”; //it’s an another way to create a String object
here tell me, are we using the new keyword? nope, right? so if we are not using any new keyword it’s not a runtime operation, so no chances of object creation in the heap. but wait, “Sourav Sarkar
“ is a constant here and where we store the String constants ? in the String constant pool(SCP) right? so an object will be created inside the SCP.
So, in this case, only one object will be created and that will be inside the SCP.
now here is a task for you.
String str = new String(“abhilash”);
str.concate(“Panigrahi”);
let me know how many objects will be created in the memory? A clue for you is concate(“panigrahi”) is a runtime operation.it’s tricky but just apply the logic.you can surely do it.
@@SeleniumExpress
Case 1 : String str = new String(“abhilash”); here this is a run-time opertion because we are creating String object using new keyword. So one object will be created in the heap.
But here "abhilash" is a constant, so another object will be created in string constant pool.
So, here two objects will be created for case 1.
Case 2 : str.concate(“Panigrahi”);
here concate is runtime operation so one object will be created in heap.
but "Panigrahi" is a constant too, so another object will be created in the string constant pool.
So, here two objects will be created.
So the conclusion is that, total four objects will be created.
Spot on !! You are awesome!! 👍
@@SeleniumExpress sir gr8...the way u explain jst awsome...
@@souravsarkar1615 not four objects it is only three ibjects because already heap had the earlier one oject so it is just point to another object it will not create another object of new keyword
I Leared today on .Intern(), By applying String. intern() on a couple of strings will ensure that all strings having the same contents share the same memory.
clear cut explanation! Highly appreciated!
By new keyword : Java String is created by using a keyword “new”. For example: String s=new String(“Welcome”); It creates two objects (in String pool and in heap) and one reference variable where the variable 's' will refer to the object in the heap.
All your contents are really good with explanation on how things internally work and that's what makes it a class apart. I see few people's comment to correct your accent , but trust me you don't have to if you want followers from other country.
I'm working in an investment banking company and i see lot of Indians who are settled in US speak the same way so that others get their accent as they (Americans or people from Europe) don't get our accent that easily.
After this vedio i got to know clearly what is mutable and also about heap and constant pool.. Thank you so much avilash 😊
You are welcome !! 😊
kindly upload videos for project of real time like Banking Application and finance domain with spring ,hibernate and restwebservices.
Very useful sir..ThankYou
Explanation was awesome sir👍👌
I recently started watching your videos! You are doing an awesome job! thanks
Hmm... I have a different thought on this. This video explains that strings are immutable because of the string pool. However, I'm going to say that it's the opposite - String pool essentially works due to the immutable nature of strings.
So, what's the actual reason for the immutable nature of strings? Here's a theory - A string is stored internally as an array of bytes.
1. What happens when a string is modified?
The byte array will most likely need to be resized.
2. What if the array cannot be resized because the subsequent blocks of memory are already occupied?
A new byte array needs to be created with a sufficient size, all bytes from the current string copied over to the new array, free up the old array eventually (GC).
Now, #2 is going to happen in almost every scenario. So, that's immutability occurring automatically by itself. Since that already happens, it probably makes sense to make it behave immutably for every string because it's probably more efficient that way, rather than handling every specific case.
Now because strings can behave as immutables almost automatically, a string pool helps in keeping a check on memory to a certain degree. Even the concept of using strings as hashes is due the same reason.
So, string pool and string hashes are a result of the immutable nature of strings and NOT the other way round.
Awesome Explanation bro!!
Thank you for clear explanation
Very Impressive, I dint know the main purpose of intern(). Concepts are very much clear to me . Thanks
Very nice explanation
I am following your all videos relay its good conception and explanation also that is the reason I am asking to you
Very Helpful video
because your explanation is very very good sir please can you do oops concept videos
explaining on board is more better than computer so try to make more videos by explaining on board only bro.. masth explaination by the way
superb bro, all I knew earlier except that intern() method part. Thanks for letting me know a new thing
Happy to know that, Shekar.
Watching a video for 20 minutes, where you already know the content is appreciable. You are an awesome learner.
Please make a tutorial series on multithreading or suggest a good source to learn the same
Your video just blow my mind.
Thanks, valuable info here!!!
Your video are seriously goo...but some videos lack clarity of voice, pls do look at it. Thanks again for wonderful explaination
Sir please create a video on stringbuffer and stringbuilder
Bro can you please upload the core java playlist also
Clear explanation
Very Use full tutorial thanks for making this
Excellent explanation on .intern()
please can you explain oops concepts videos with real time explanation and examples with clear concepts
Can you please explain on Completionstage and completablefuture topics
Dear instructor,
I have two questions,
1. What is String pull in general? is it an object in heap memory yes?
2. Nevertheless the comparison of String s = ""ABC" vs String s = new String("ABC") is already discussed, but in one tutorial instructor mentions that even when we create String in String s = new String("ABC") way the object is created in heap but that object points to the "ABC" value in string pool, is this what happens behind the scenes? Or when we do new then a String object is created only in heap which holds ABC value? So if string pull is empty and we create new String("ABC") object then it will be created in the heap and string poll will still stay empty?
Thanks
Avi you are doing a great job. You are very clear with the point that you explain. Just one suggestion if you can consolidate all the things together in one real time project. Very few person come with such an clear explanation.
Hi Rajnish,
Thanks for the suggestion. I will be having an another video on my channel where I will cover how immutability of string help us in a multi threading environment and in web world. I will try to come up with some real-time stuff.
Will be waiting eagerly for the stuff.
Hey Thanks a lot Avi. Your style of explaining the concepts is really very good. You go in depth to explain the concept. Really appreciate your efforts. Request you to start a series on Collection Framework and also on Java 8.
Plz create videos on LinkedList, LinkedHashMap, HashSet..
Hi Avi You have clearly explained the concept. great job. I 'have also gone through all the comments below.. You explained all the queries with care. Nice will surely browse through your videos.
Thank's for the kind words and I am glad that you found it clear.
Happy learning!!
As You've said I absolutely cleared on immutability of String and thanks for ur reply
Please make video on why java is pass by value and not pass by reference ..
I will do, Yoshita !! Thank you for Suggesting.
Great explanation 👍 I have one question .. what happen with heap memory objects when are you using intern method is it for garbage or else?
Plz do video on java threads
have you covered OOPs(abstraction/Inheritance/Polymorphism/Encapsulation) concept in one video kindly give me the link..
#progrank just go ahead find there
please can you do the Multi-Threding videos in Java. I am facing this issue so many times in Interview
But I couldn't clear this type of quation in Interview
@Selenium Express have some query related to String immutable
public class Main
{
public static void main(String[] args) {
String s="Selenium Express";
String s1="Selenium Express";
s=s.concat("RUclips");
}
}
in this case after i concat "RUclips" to s and assign it to reference s new object will be created in Heap and also in String Constant Pool ,will s ="Selenium Express" is still present in SCP or s reference will be removed ? .
Isn't we are modify same string s
Thanks
In the following example I am getting the output as "References are unequal' but i am expecting the output as "References are equal" since S3 and S4 are created using string class. Can you explain me this
String S1="Mary";
String S2="John";
String S3=S1+S2;
String S4=S1+S2;
if (S3==S4)
SOP("References are equal");
else
SOP("References are unequal");
Sir, how does garbage collector works in string constant pool?
Since you said that string objects are cached in SCP, there should be some provisions to delete the objects present in SCP which are no longer in use or else we will eventually run out of memory.
very nice, well explained appreciate!
number of time I got oops concept question but I am not clear on that sir. please can you do that videos sir
Abhilash Bhai, please make a video on spring jdbc with depth and internals with suitable examples....
Sure, Santosh.. Will do.. Now a little busy with the upcoming core videos. Once done, will definitely make a series on jdbc.😊
super explanation panigrahi..keep it up with u..and u r from odisha i guess.so proud to be odiya.
Thank you for appreciating, Prasanta.Glad, you found it helpful.
And yes, you guessed it right.I am from Odisha.
Please can you do this video
How to remove the duplicate values in A string or Integer without using any default methods?
Can you please explain about String s="raja" and the same variable we are taking s="rao"; and we print sop(s); then why the content is changing.
Can you please explain how the memory is decided or checked inside SCP or heap weather the object is present or not?
Eg String str=new String("avi"); it will create object in both heap and SCP?
and String s1= "avi"; will point to same only?
i have question for is java is call by value or call by reference?
Not only for memory constraints and further more like sharing properties for the different objects
Hi could you please prepare one video about comparable and comparator please
String s = new String ("abc");
s.concate("xyz");
It's create new obj or ?
Excellent!
Hi Avi, What is the use of creating new String() in the heap area if we have already string literal in the SCP. Can you please explore over this.
Hi Vinay,
You right on your point. Using " " you can create a string on the pool directly which is memory efficient. But using new keyword or by using constructor you too have an option to create an string object.
The time you are going to need it, think about an situation when you need to convert an Char array to a string, you might need an constructor of string class which accept a char array, to convert that array to string.
Eg
char charArray[] = {'a','b','h','i'}; String convertingCharArray= new String(charArray) System.out.println(convertingCharArray); //abhi
Like that , we can get few different constructor of String class which is used to construct a string by passing different argument.
Please refer the below url which will redirect you to the string api and find the constructor section .
docs.oracle.com/javase/7/docs/api/
Hey Avi i have one question.
Why our java file name must be the same as public class name ?
We can take any number of classes in a java file why only one public class is allowed and whose name should be class name ?
i hope u will answer my question, coz i have been asked this question in many interviews but im not getting clarification any where else.
String s1 = new String("AB");
String s2 = "AB"
Total 3 object would be created ?
as per my understanding only 2 because first line creates AB in heap as well as scp, second line will not create AB in scp because AB is already there in scp so s2 will just point to already existing AB . simple
Badhiya kahucha bro...carry on..feel proud on you
Thanks bhai!!
nice explanation. can you also do videos on selenium interview questions please
public class Main {
public static void main(String[] args) {
System.out.println("Thanks Selenium for the tut.");
String first="Testing SCP",
second= new String("Testing SCP"),
third= new String("Testing SCP").intern(),
fourth= "Testing SCP";
System.out.println(fourth==first); //true
System.out.println(fourth==second); //false
System.out.println(fourth==third); //true
}
}
Bhai Hibernate bootstraping 3.x,4.x 5.x upare gote bhala video baneiki upload kara....
Hey Avi. Good description of the concepts. I wanted to know the use of intern method. What;s the use of creating an instance with new keyword if we pointed it to scp using intern. We could have directly created the string using literals right?
Hi Manmeet,
You right on your point. Using " " you can create a string on the pool directly which is memory efficient. But using new keyword or by using constructor you too have an option to create an string object.
The time you are going to need it, think about an situation when you need to convert an Char array to a string, you might need an constructor of string class which accept a char array, to convert that array to string.
Eg
char charArray[] = {'a','b','h','i'}; String convertingCharArray= new String(charArray) System.out.println(convertingCharArray); //abhi
Like that , we can get few different constructor of String class which is used to construct a string by passing different argument.
Please refer the below url which will redirect you to the string api and find the constructor section .
docs.oracle.com/javase/7/docs/api/
Feel free to post an another question if you have one.
Could you please recommend books for comprehending these concepts
Hi bro, is that only string data type uses string constant pool.. what about other data type..
Hi.. please explain.. below mentioned code.. I have little confusion.. String a="hi"; a="hello";
In this situation string a value to be changed.. so how is it possible? Please solve this asap.. thank you
Hi Avilash !
According to this video. I like this explanation, but I have small doubt please do clarify.
As we can see that you saying because of "String Constant Pool" the "String is Immutable". Is this correct because I heard that "String Constant Pool" is introduced after "String Immutable" introduction. So how can you tell that because "String Constant Pool" so on so process "String is Immutable". Please do clarify on this point.
And I assume that String is Immutable because we use the String value as an key in hashmap which is popular !!!
You are correct, string is immutable for many reasons.
As you said its used as a key in hashmap.
It's too, popular for hashcode caching as well.
String immutable nature also helps us to use it for security purpose. For an example, think that we are using it as parameters in DriverManager.getConnection().all parameters inside that methods like url, username, password are Strings.
Even if a hacker wants to change it ,he will end up creating a new object as string is immutable.
String is immutable, so by default it's thread safe.
it has many reasons like this.you can verify it in Java docs.
How is the equality and hashcode methods overridden in strings to differentiate between strings created as literals and those created using new keyword ?
TBh I just found 1 reason why strings are immutable. And that's because of the pooling functionality. What are the rest ?
V nice no suggestion but why channel name is selenium I avoid ur channel I thought it's related to testing
Sir Please do a video on RESTFUL webservices .. Thanks in advance :)
#ProgRank just go ahead on that channel
Hi Avi, regarding intern() method, if s1 points to the reference in SCP, then a new object will still be created in Heap and allocates a memory for it with the same content ?
Hi Mohan.
Yes, If we will create another string object with the same content again(using the new keyword), then it is going to create a different object in the heap. To point that object to SCP, again we need to use intern method.
Please refer the below code.
String str = "Avi";
String str1 = new String("Avi").intern();
System.out.println(str == str1);//true
String str2 = new String("Avi");
System.out.println(str==str2);// false
Please Keep me posted, If this answer doesn't help.
Great. I got it..,thanks for your reply :-)
@@SeleniumExpress Hi Avilash, thanks for above code, it also help me to understand how "==" and ".equals" are works
Bhai tame kn odisha ru?
@@SeleniumExpress Hi Avilash, quick question, so why would we want to do String str1 = new String("Avi").intern() when the reference is going to be pointing to the string literal in SCP and not the object created in heap? Wouldn't this object get garbage collected since there is nothing pointing to it. P.s. Thank you for your videos, you are so amazing!
so can i modify objects which is in heap?
What if i had String str = "Jon" and String str1 = "jon" i take that they will be two difference ref variables?
Dear I have question
1.
String s = 'avi';
String s1=new ('avi');
sys(s.hashcode());
Sys(s1.hashcode());
Output -> -1200,1200
You say s1 is created in heap memory and s is created in SCP then how we get false if both are pointing to same memory location
2. If I use intern() then I get true I am not understanding this scenario ..plz respond me
Hi Aniket,
Thank you for posting a comment.
So when you create 's1' object, you used the new keyword.As object creation is done at runtime, the s1 object goes to heap area. and 's1' is pointing to the object which is there in heap area.
when you create 's' object, you have use double quote or " " to create the object.When you create an object like this(using " "), it's called a constant, so it goes to SCP.
so in this case, an object will create in SCP and 's' is pointing to that object.
So in the above scenario, two different objects got created one in heap and one in SCP respectively. so these are the different object. 's1' and 's' are not pointing to the same object.That's why you got false if you compare them (s1==s2) .(double equals (==) checks reference)
Don't be confused with the hashcode.hashcode is coming same because both the content is same.but what matters is how you are creating the object.Is it by using new keyword or by using " " .
Question 2 :
when you interning an object by using the intern method the reference is moving to SCP.
for an example
if you create an object like below
String s = "aniket";
then an Object will be created in SCP (as you have used "" to create the object) and s will be pointing to the object in SCP.
And again when you are trying to create an object by using a new keyword like below :
String s1 = new String("aniket");
an object is getting created in the heap as you are using new keyword and no other object is going to be created in SCP as 'aniket' is already present there.
and as I described you already, if you are using new keyword the reference is going to point to heap.
but if you want to move the reference to SCP or if you want to use the object 'aniket', which is there in SCP then you have to use intern().
for an example :
String s1 = new String("aniket").intern();
now the reference will move to SCP .
so both 's' and 's1' object pointing to the same object in the SCP and you get 'true' as a result if you compare them (s==s1")
let me know if you don't get me.you can refer the below video for string object creation.
ruclips.net/video/xldyBBdpknM/видео.html
keep me posted if you have any further queries.
Have a nice time Aniket!!
@@SeleniumExpress Really nice explanation. Thanks! :)
Hi, can you suggest what will happen with below code?
String str="avi";
str = str.concat("lash");
In this string allowing me to mute the object, may i know the reason?
Its because ur using the same refernece str while concating it.
While using. concat() method, new object is created in heap memory and not in String Pool Constant area. Why?
this is what the power of hyderabad.. i like it
What will happen if I use intern() and the string will not in string constant pool?
@seleniumexpress Are you sure that String s = new String(“Avi”); automatically creates string object in SCP? I dont think this is true because then the concept of “Interning” string objects of heap into SCP would then be unnecessary.
Kindly respond.
Hi can you please upload a video for spring, maven and junit test integration with this web app using some use case with database, ex : online movie booking and cancellation
String s="sudarshan";
String s1="varun";
String s="damodhar";
Sysout(s);
what it will going to print please explain this?
Compile time error we cannot create object with duplicate name
It will print damodhar because s will reference to damodhar when you give String s = " damodhar"
Which operation's are run-time and what other?how?
How string will work in multithreading
Hello Avi its nice ,Will be much better if you can explain same in compiler .
Sure, I will put it's practical soon.
Have a nice time !!
Try to make it short videos .. taking so much time to explain the concepts
This is also the reason why rename refactor works as it does
Bro, garbage collector don't access String pool area,you said object is eligible for garbage collector
Great man....
I have a small question related to intern() API. As you said that this API forces the JVM to point the address available in SCP even though we use the new keyword. Can you please help me to understand, String s= new String("Rajnish").intern(); Is that the memory created in heap area is ready for GB collection.
Rajnish Sharma Yes absolutely, the object which was created in heap will be no more in use as u have used intern().
So it will be ready for garbage collection
Hi,
Thanks for the tutorial
str2.concat("lash") has to be in String Constant Pool,not in Heap right?
"Lash" will be created in the scp are as it is in literals.but avilash will be created in heap are as we are using concat() and it is a runtime operation only if it has a reference like...... String s2=str.concat("lash").
hi.. great content, but as you said a new object is being created in the heap section while using new String("Avi"). It means now the reference is not to the SCP so we should be able to mutate the string obj in that case. ??
so why when i call hashCode on
String s = new String("Abc")
and Strin s2 = "Abc"
hashcode and equals is true ? its same object, so ... there is not s on heap
please make your videos a little bit shorter length...thanx
sir can you make video on .equals() and hashCode or explain it
yah sure, I am working on it right now. Will be published soon.
Thank you so much for suggesting..
Hi Avi, Its long we have not heard you. What's the next you are uploading for your student? Missing your videos.
Hi Rajnish, hope you are doing well . I am a little held up with work. I will be uploading soon.
what will happen if I give str2=str2+"lash"?
Mast
Thank you sid
u r awesome
make video short it takes long time but its not required