Why string is immutable in java || The 4 reasons you must know || part 1

preview_player
Показать описание
Well, Why String pool? What is the need of String pool in java? How is string being an immutable class helping us optimizing the memory uses in java?

That's what we are going to learn in this tutorial.We are going to cover one of the most common FAQ why String is immutable in java? There is not a single reason for this.String class is designed as an immutable class by keeping different things in mind.Today we are going to cover one of the most important reasons why the string is immutable.

Immutable classes object doesn’t change their state.The string being immutable can’t be modified once string object is created.One of the reasons why it is possible to have a string pool inside the heap memory.In string pool, once an object is created, it is cached in the memory.After that, no object with the same content will not be allowed to the string pool as duplicates are not allowed inside string constant pool.Multiple strings can be referred to the object which been created inside the string constant pool which helps us to optimize memory and help us to stop creating tons of object.

String pool implementation is one of the biggest reason why the string is immutable in java.

We are also going to learn about how to intern an object inside string pool in this tutorial.

In the next video, we are going learn about one of another reason why the string is immutable.I am talking about string hashcode caching here.The reason why we can use string a key in the hashmap.

Below link will redirect you the next tutorial
String hashcode caching (One of the biggest reason why the string is immutable)

subscribe to my channel by clicking on the link below.

Stay tuned and like my Facebook page for more.

Music :
-----------
credits : -
(intro)
1)
Creative Commons — Attribution 3.0 Unported— CC BY 3.0

-----

intro template :
Рекомендации по теме
Комментарии
Автор

String s = ""ABC" vs String s = new String("ABC"); Can u explain this?

souravsarkar
Автор

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!

Kumarkumar-gfkk
Автор

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.

albeliever
Автор

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.

pogou
Автор

kindly upload videos for project of real time like Banking Application and finance domain with spring, hibernate and restwebservices.

PankajKumar-qkoh
Автор

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

arjuarju
Автор

Bro can you please upload the core java playlist also

ashakankanampati
Автор

I recently started watching your videos! You are doing an awesome job! thanks

rawatpraveen
Автор

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.

shashankmm
Автор

After this vedio i got to know clearly what is mutable and also about heap and constant pool.. Thank you so much avilash 😊

akulasarika
Автор

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

pavithram
Автор

I am following your all videos relay its good conception and explanation also that is the reason I am asking to you

rameshbabu
Автор

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.

amolnawale
Автор

superb bro, all I knew earlier except that intern() method part. Thanks for letting me know a new thing

shekark
Автор

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

vv
Автор

number of time I got oops concept question but I am not clear on that sir. please can you do that videos sir

rameshbabu
Автор

because your explanation is very very good sir please can you do oops concept videos

rameshbabu
Автор

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.

Rajnish
Автор

@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("Youtube");

}

}


in this case after i concat "Youtube" 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

umeshjoshi
Автор

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.

saurabhkaranjkar