Podcast Lookup

Java Strings Explained | String Pool, Immutability & Internal Working of Strings

The Java Developers Hub

  • December 3, 2026
  • 1 h 9 min
  • Episode 31
Episode page
About this episodeStrings are among the most frequently used data types in Java programming. From storing user input to processing text and managing…
Strings are among the most frequently used data types in Java programming. From storing user input to processing text and managing application data, understanding how Java handles strings is essential for every developer.In this episode, we take a deep dive into Java Strings, exploring the String Pool, string immutability, and the internal mechanisms behind string storage and manipulation.You'll discover why Java Strings are immutable, how the String Pool helps optimise memory usage, and the differences between creating strings using string literals and the new keyword.Whether you're a beginner or an experienced Java developer preparing for technical interviews, this tutorial will help you understand how strings work behind the scenes.What You'll Learn
  • Understanding the String class in Java.

  • Different ways to create String objects.

  • Understanding the Java String Pool.

  • Differences between string literals and objects created with new.

  • What string immutability means.

  • Why Java Strings are immutable.

  • How immutability improves security and performance.

  • Understanding String references and memory allocation.

  • Differences between == and .equals() when comparing strings.

  • Understanding the intern() method.

  • Exploring common String methods.

  • Introduction to StringBuilder and StringBuffer.

  • Differences between String, StringBuilder, and StringBuffer.

  • Best practices for efficient string manipulation.

Who Should Watch?
  • Beginners learning Java programming.

  • Computer science and software engineering students.

  • Java developers preparing for technical interviews.

  • Backend developers working with text processing.

  • Developers interested in JVM internals and memory optimisation.

  • Anyone looking to master Java fundamentals.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Strings Explained | String Pool, Immutability & Internal Working of Strings

00:00—
  1. Speaker 1

    Yes, so let's start. Welcome back Coder Army to our Java series. And today we are going to understand in-depth about strings. How do we make strings? What kind of strings are there? What functions are there? And how do strings look internally? We will understand it well. This is very important for the interview point of view. But it will make your Java strings knowledge so good that you will never have any problem with strings. So let's start it quickly. Yes. Before we understand strings, we need to understand characters. Obviously, in programming, we declare characters. What is a character? It's a symbol that a human can read. For example, a is a character, a is a small a. We have all the alphabetical characters. Similarly, from 1 to 0, we have all the characters. And we have special symbols like dots, commas, etc. Everything is characters in Java, which you can type with a keyboard. In fact, in any language, any alphabet is a character. Okay, now we know what characters are and we also have a char data type. Which is a primitive data type that can represent a character. Now, what is a string? So, I say that string is nothing but a sequence of characters. So, I can write it as sequence of characters. Okay. So, it is a sequence of characters. Okay. So, basically, what does sequence mean? In programming, whenever we talk about a sequence, meaning, we are talking about a collection, right? And if we want to store a sequence, then generally, which data structure do we have? We have arrays, right? So, I can say that string is nothing but a character array, right? So, if I make an array of a character, String is an abstraction of the character array. If I want to store my name in a character array, it will look like this. And in this way, I will store my name in a character array. And I am saying that this is a string. An abstraction on it is a string. Now if we go in a little depth, then we will understand that these characters will not be stored like this in the program. Right? How will they be stored? we get a unicode value of all these, which we store in the program. So basically, let's say A is 97, so I will store 97 in the program. Similarly, something will happen for D, for I, for T, for Y, and for A. I will get a unicode value of all these, and I will store it in an array. Very good. So, if we have a string as a character array or an abstraction, then why did we get a string as a data type in Java? We could have used a character array everywhere. Think about it. Why didn't we use a character array in any programming language? Why did we get a string as a data type in Java? There is a very simple reason for this. If we use our simple character array everywhere, then we don't get a lot of functionalities. For example, we perform comparison in strings, we don't get that in any character array. You could have done it, you would have to code everything manually, you would have to write its algorithms. So if I want to concatenate a string, then I would have to write a different algorithm manually for it. If I want to find a sub-string of a character, then again, I don't get that directly in the character array. What do I have to do? I have to write algorithms for that. So basically, what I mean to say is that a lot of functionalities that I don't get in the character array, where do I get them? I get them in my strings. How do I get them? Because what does a string do? It puts an abstraction on it. Basically, our primitive datatype character is taken to a non-primitive datatype string. And you know what non-primitive means? What will a string be? Obviously, it will be a class. It will have some methods, some fields, some constructors. And what will we get because of the methods? We will get a lot of properties. Okay. So, basically, it didn't happen like that. Like we have int, char, float are our primitive data types. But again, in respect to this, we have wrapper classes. For int, we have integer. For char, we have character. For float, we have capital float. So basically, for every primitive data type, we have a non-primitive class. So it's almost the same that we have a character array and we put a wrapper on it and we made a string. Now, this is not the same because arrays are a non-primitive data type. So, character array comes in a list of non-primitive data types. But obviously, we have a lot of functionalities in string that we want to do, which is not possible to do on character array. As we just saw, we can't do comparison. Means, we have to write algorithms to do it. And you know, string is one of the most used data types in any language, right? Because you think, whenever you write code, You store names, passwords, e-mails, urls, database urls, everything in strings. So string is a very useful data type. So obviously we need a lot of functionalities for that. What can we do? We can keep it inside a string class. For this reason, we will not have enough character arrays. We will have to go above that, we will have to put an abstraction layer, which we call strings. So we have understood that our string is a non primitive data type. Now if the string is a non primitive data type then it will be a class and it will come in a package. It comes in the java.lang package. In this we have a class named string. So this is our package name, our class name. Now we have talked about java.lang package many times. This is the package which is auto-imported, right? In this only our system class comes, in this only our object class comes. We have already seen many classes in java.lang package. Similarly, strings also comes in our java.lang package. Means to use strings, you don't need to import anything because it is auto-imported. Okay. Now let's think, if string is our class, So obviously in Java, a code will be written class string, right? And there will be a lot of things in this class. There will be some variables, some constructors, and some of our methods, right? So basically, if we have to make an object of our string class, then we know how we generally make an object. We will make it like this. String s equal to new string. We will make it something like this. Right? This should be the only way to make objects. But we have also seen that we can write it like this. String s equal to hello. Since we are working in strings, we declare the string like this. We define it like this. We don't use any new operator. Is Java hiding something from us? Because we always know that we use new operators. But in the integer class, we used to write integer i equal to 4. And we knew that Java does something like this from inside. It puts new integer there. And it will pass 4 in its constructor. Right? This is the old Java method. What is the new Java method? We know that we call a static method in the integer class. Integer.valueOf And we pass 4 there. Right? So, basically, what do we expect? That the valueOf method will call this constructor from inside. Right? So, somewhere this new operator is called. Whenever we make an object. Because our objects are called in heap memory. And to make anything in heap memory, we need a new operator. So, when we write this syntax. String s equal to hello. So, what will be happening? Obviously, some abstractions will be happening from inside which Java is hiding from us. Somewhere, a new operator will be being used. Now, how is this declaration happening? Let's come to this after some time. We now understand that what is the property of our string class, which is its most important property. Our string class is an immutable class. We already know what immutable classes are. They are classes that cannot be changed. What does it mean? First, we declare it final so that no one can inherit that class. We finalize all its methods so that no one can override them. We remove all setters because we don't want anything to be set through setters. We only keep constructors and getters. Right? We have studied how to make an immutable class, why it is there, etc. Now in Java, because our string class is immutable, we can never change it, we can't change anything. Okay? Now first of all, what do you think, why did Java keep strings immutable? Look, think about it. As we talked about strings, it is the most used data type, right? Everything, let's say our passwords, okay? Let's say URLs, whether it is a database URL or an application URL, okay? You can call it as hashes, you know we use hash for everything If we want to use some data structures Like in collection frameworks we want to use set Hash set, hash map, we use hashes internally. So basically for a lot of things, we need string data structure. And we don't want our string object to change. Because if your string object was mutable, then your passwords could have changed, URLs could have changed, hashes would have changed. So our entire hash set, hash map infrastructure would have fallen. They would have never been able to work. So, Java has already thought that the string should be immutable. Once you make an object of the string, it can't be changed. You can make another object if you want another string, but the original object will never change. So, first of all, let's see the immutability of the string in the code. I have taken my demo.java as an empty project. So, first of all, I know that I don't need to import anything to declare the string. So, I can write the string directly. And let's say I write string s1, let's say hello. Hello. Okay. Very good. Now if I try to concatenate something in my original string S1. Do you know what concatenate means? I will try to add a new string behind this string. So I have a function in the string which is called concat. Now we will come to the functions of the string easily. But for now, assume these are functions. So let's say I am going to write world here. So what did I write here? Space and world. So I am expecting that hello world should be printed in s1. So now if I try to print this system dot out dot println and here I will write s1 so let's see what our print is so let's see what our print is so see my print was only hello which means our original string hasn't changed at all right I did s1. concat world but our original string s1 reference still has hello so this proves that our strings in java are immutable you can't change them Now you will ask, what happened to this? We tried to concatenate this. So the answer is, nothing happened to this. We declared a new object in Java. So now there is a new object named hello world reference, which no one is referring to right now. We will see that too. But I hope you understood that our strings in Java are immutable. Now, let's move one step ahead and see how we declare a string. So far, we've seen that we can use a new operator, right? And we also assign a string by doing equal to. But what's the difference between these two? So look, in Java, you can declare strings in two ways. One is using literal Literal method and the other one is using new operator. Okay, we have both the methods. Let's see the difference between both of them. And we will understand that our literal method unlocks a new thing which is a very interesting feature of Java. Which we call string pool. Now this is a very important and very interesting concept of Java. Because of which the strings in Java become very optimized. Okay, so let's see it once. Now look, you understand it like this. Because we said that string is our most used data type. So if we keep making a new object in memory, then our memory will be very full. We will be making a new string for everything. And in general, we use millions of string. Millions of string. For URLs, for hashes, for passwords, for storing normal names, for storing anything, we generally prefer strings. Other data types are used, but they are used less, as much as strings are used. So, if we start making a new object to declare every string, then it will be a big problem. So, look, what generally happens? Generally, you know, we have a heap memory. And we have a stack memory. We have understood this many times. This is stack and this is heap. And whenever we declare an object, you know that object is created in our heap memory. Let's say we make its reference s in the stack memory. And that reference points towards that object. And if we want to call any method of this object, then we can call s. and whatever is the name of its method, let's say fun, something like this. Right, in the case of string, string gives us two ways to declare it. One is the literal way and the other is the new operator way. First of all, we are going to see the literal way which is very interesting. Now see, whenever we declare a string in Java in this way, if we write, let's say, string s equal to hello. By using simple equal to operator, we did not use any new operator here. We call this literal way. A literal way to declare a string. Because we didn't use a new operator. Okay. Now let's first understand what Java did. It reserved a very special place for our heap memory. Something like this. Which Java called String pool. What did you say? String pool. Okay. What is string pool? Java said, because we have millions of strings, right? And every time if you keep making a new object, then there will be a problem. So, what will I do? I will try to reuse strings. So, as soon as you wrote this line, let's say you wrote string s equal to hello. So, what will happen? Our string object will not be in normal heap memory, but will be in this string pool. This means that an object will be declared in our string pool. What will be inside it? Let's say hello Hello. Okay. And obviously its reference pointer s will be in our stack memory. And s will point towards this hello. Okay. Our hello will come in this string pool. So first of all I will make this pool a little bigger. Okay. So I made the pool a little bigger. Now the benefit will be that let's say you are going to declare a string again. So you wrote string s2. So first let's call it s1 and make it s1 as well. s2 equal to and you wrote hello again. Something like this. So Java will think that you have written hello again. Now if I make a new object for it, write hello in it, and assign it to s2, then I have declared two objects which have the same name, right? They are basically the same strings. If I make a third hello, then Java will make a third variable, and it will keep its reference in the stack memory. So again, the more strings I make, the more memory I will fill. So Java will start reusing here. So what will Java do? As soon as you write string s2 equal to hello, First of all, the s2 reference variable will come in the stack And java will check that our string pool Is there a string named hello in it? The answer is yes So what will java do from inside? It will also point this s2 on this hello That means we have reused the strings Now you know that if I do a third string again, hello So again, S3 will go to our stack and it will also start pointing to this string. So now these three variables S1, S2, S3 are pointing to the same string. But if I make a fourth string, let's say S4, and this time it will be different, let's say it will be world. So what will happen again? We will keep S4 on the stack. We will see if the world name already exists or not. So we will make a new object of world. World, something like this. And S4 will be assigned to that world. Now, if someone declares a world in the program again, then that too will point to this world. So, this is called Java's string pool, in which you start reusing strings. You don't make a new object every time, but you reuse old objects every time. Now remember one thing, this java string pool is now a part of modern java heap memory. Previously this part was not even present in heap memory. It used to be a part of a special memory called premgen. But now in modern java it is a part of heap. So I have shown here that there is a part of heap in java which we call java string pool. So this was the first method of declaring a string pool. Now what is the second method of a new operator? It is exactly the same as we used to declare an object. So let's say again I create a heap memory and a stack memory and if I write something like this String s1 equal to new string So I will put new operator And suppose I pass hello in its constructor Which I basically have to store Now what will happen? Now what I expect will happen In our normal heap memory An object will be created in which hello will be stored And its reference pointer will be created in stack memory And it will point it Now suppose again a variable is declared s2 And in it also same thing is stored Let's say it also writes new string Hello, then what will happen? We know that nothing happens in heap memory that it uses the same reference. Our new object will be created again. The content of hello will remain in it. And what will S2 do? It will point it. Now, every time you make hello, what will happen to us? A new object will be created. This is the basic method that we had understood till now to declare objects. So, this is our second method by using the new operator. Now you must be able to understand the difference between both. Here we are sharing the references and here we are not sharing the references but we are giving a different object to everyone. Now if you understand this well then we already know that in our equal operator and in our equals method which comes from our object class, what is the difference between these two? Equal equals generally compares references and equals Object class only compares references. But if a class has overrided it, then it compares the actual value. And our string class has overrided this equals method. That means we always compare values, not references. So let's go to the code and see how it works. Yes, so we are back in our code. So let's say I remove this, concite. So I have made one string s1 hello. And I make another string, let's say s2. And I will also put the same hello in this. Okay, I have kept both the variables the same. Now see, because I have used equal to operator here, so what is the way to make Java strings? This is the literal way. Now you think, what will happen if I try to print, if s1 is equal to s2. When I write equal to equal, then I ask Java, are the references of both of them the same? Right? Now, since both of these are in the string pool and we know that memory is reused in the string pool. So, both S1 and S2 will be pointing to hello. What will happen? For example, string S1 equal to hello will be written for the first time. So, the object named hello will be made in the string pool and will be pointing to S1. So, when S2 equal to hello will be written again, then JVM will check whether the object named hello already exists in the string pool. If it does, then S2 will refer the same object. So, the answer to this is expected to be true. So, let's see that too. Now let's see the next method. Let's take two strings S3 and make it a new string. And now I write Aditya in one and take another string S4 and I put Aditya in it in this way Now here I have used a new keyword, and you know what a new keyword does? It makes an object in a normal heap memory. Now the object it makes in that heap doesn't reuse the object, it makes a new object every time. So this means that if I do system.out.println and write s3 is equal to s4, then our expected answer here is false. Because their references are different and equal to equal compares references. So once we run this, the answer is false. The first one is true, the second one is false. So what we expected is exactly the same. In Java, there is a way to make strings, which we call string literals. And there is a way which we call using new operator. So I will write here using new operator. Now if you understood this well, then you have to remember a golden rule. What is the golden rule? First of all, this rule is very important because if you understand it well, then you will never have any confusion on strings. Now it says that all the strings that are made from compile time constants go directly to the string pool and all the strings that are made from new operator go directly to the heap memory. So, first of all, I'll write that rule. So, the rule says that only compile time constants go to the string pool and run time created strings go to heap memory. So, we saw that all the strings that we were calling from new operator go directly to the heap memory. Apart from this, all the strings that are made on run time will go to the heap. And whatever will be your string at compile time, that will go into your string pool. Now when we see its examples, you will understand everything. There are a lot of interview questions on this. A lot of tricky questions are tried to confuse in this. But we will see it one by one. You will never have a problem in strings all your life. Now we are going to see some examples of this rule, that we talked about. Compile time constants will go in string pool and run time will go in heap. Okay. So you imagine, that I have declared two strings, string s1 and string h2. So let's say in s1, I write ja plus va. Something like this. You know what a plus operator does? Plus operator concatenates two strings. So if we concatenate ja and va, then our string will become java. Right? And in s2, I directly write java. So what do you think? When I try to print on the screen that is S1 equal to S2K? So what will be the answer? True or false? The answer is that we will get true. Why? Because look, this is what we call compile time constants. These different strings, J, A and V, A, because we have literally defined them here, right? So, at compiler time, they will be concatenated in the same string, which is Java, and its reference variable will be stored in S1. And where will our Java be stored? In our In string pool, same when I make S2 again What am I trying to store in S2? Java? Our JVM will see that does Java already exist? The answer is yes So S2 will point to it, S1 will also point to it So it means that if this is our string pool Then our Java string will already be made Through S1. So S1 will be in our stack memory and Java will be already made here. Why? Because we have already joined these two strings together at the compiler time. Now when my S2 reference variable will be made, it will also point to this. So here the answer should be true. Okay. So I will write it here as true. Now if you understood this, then you can try to answer the second one. So if you understood this properly, then this was our example of compile time constants. Compile time constants. Okay? Because you know that JA and VA will join at the compiler time and make the same string. Okay? Now if I see the example of run time constants, then it looks something like this. So let's say this time I take a string S1, in which I only put JA, and I take a string S2, in which I write whatever is in S1, plus VA. So what will happen in this case? Since JA is a literal, it will be easily assigned to S1 at compile time. But S2 is saying that what I want is whatever is in S1 plus I will add VA to it. Now, this can't be decided statically i.e. at compile time that whatever is in S1, I will directly add it to VA. All these things are decided at runtime. When our code will be run, then our compiler will find out what is the exact value of S1 which is JA. Then it will link it with VA and then our final string will become JAVA. But all this will happen at our runtime, not at our compile time. So whatever happens at our runtime, Where does that string get created? In the heap memory. So, what will actually happen? If I explain it to you carefully. So, let's say this will be our heap memory. And this will be our stack memory. And I will make this part of the string pool. The upper part is the string pool of java. This one. And as soon as I write that string s1 is equal to ja, you know what will happen? First of all, an object will be declared in the java string pool, in which the value will be ja and its reference variable will be s1 and it will point to it. So far, everything is fine. So far, we have understood. As soon as the next line starts executing, Java compiler will see s2 is equal to s1 plus va. Since s1 decides on the runtime, we can't put it directly in the string pool at compile time. Now I'll tell you that string builder uses it from inside which we'll discuss later. But now you understand that since it will be dynamically decided, Java can't put it in the string pool. Since it won't go on compile time, it will go on run time. And what will happen on run time? An object will be created in our normal heap memory. The value of which will be java and s2 will point it. So, it will be something like this. Now, there is one more thing here. Because when I wrote s1 plus va, va is a string literal and this is also a literal. And whenever java sees a literal, where does it put it directly? Inside the string pool. That means, That means another string will appear here, VA. Obviously, no one will be referring to this string because we haven't referred to VA alone. But the string VA will also exist in our string pool independently, whether someone refers to it or not. Now, obviously, if no one refers to it for a long time, then Java's garbage collection will come and clean it. But Java's garbage collection runs slow in a string pool. We will see that in the garbage collection video. But for now, let's assume that jA was a literal and came in the string pool. vA was a literal and came directly in the string pool. jA is being pointed by s1, but vA is not being pointed. Now s2 is saying that I have to make a string by mixing s1 and vA. Which will be Java. But because a variable is written here. We will get the answer of this at the runtime. So whatever the answer is at the runtime, then where does Java make it? In the normal heap memory. So our S2 will be made here. Now if you understand this, then we will also understand that now if I take a third string S3 and put Java directly in it. Java. So obviously if I have put it in a literal way, then where will it be made? In our string pool. So here somewhere our string will be made in which Java will be there. I am writing this small. And S3 will point this Java. Now if someone asks me in code that is S2 equal to S3? So although in both, Java is safe, right? S2 is pointing to Java and S3 is also pointing to Java. But we are not comparing their values, we are comparing their references. And because both have different references, one is pointing to heap memory and the other is pointing to a string pool. So the answer will be no, which means false, right? I hope you must have understood that basically what goes in the string pool and what goes in our normal heap memory. You just have to remember that whenever we write an exact literal, it will come straight out to the string pool. And whenever we do a dynamic calculation, that is, if we concatenate a variable with someone, it will go straight into our heap memory. Obviously, if we use a new operator, it also comes into the heap, right? Now, let's move forward and see another example of it. Now, let's say, I ask you a question again. I wrote, string, S1 and in S1 I have put java and now I will make another string S2 and I have written S2 is equal to S1 now what will happen? now nothing will happen you must have understood this is our heap memory and the upper part of that heap memory is our string pool and this is our stack memory something like this so as soon as we have written this first line what will happen? an object will be made in our string pool In which the value will be java And the reference variable called s1 Will point it Now, as soon as we write the second line, string s2 equal to s1. Now, because here there is only an equal to operator, assignment operator. And we are not performing any calculation here. We are not concatenating s1. Right. So, all these things, assignment, again, where does it resolve? It resolves at compile time. Right. If these things resolve at compile time, then what will s2 do? Where will it keep its string? In our string pool. And s2 is saying, I want the same thing which is in s1. Java So JVM will check if the Java string is already in the string pool. If the answer is yes, then the reference variable S2 will point to this Java. Now both S1 and S2 point to the same Java reference variable. So again if I ask you if S1 is equal to S2, then its answer will be true because both point to the same reference. Now let's move ahead and answer one more question. We will see all these questions in the code as well. Now I am just clearing you that compile time constants go to the string pool and how do they look? And the runtime things that are made on runtime strings go to our normal heap and how do they look? So the next question is this. So let's say I have written here string s equal to let's say hello. Something like this. And then I write s equal to world. So I didn't concatenate anything here, first I wrote s equal to hello then I wrote s equal to world. So basically what I did is I replaced hello with world. So what will happen now? Let's think again. This is our heap memory, this is our stack memory. The upper part of heap is our string pool. So as soon as I try to execute the first line, what will happen? An object will be created in java which will have the value of hello and its reference variable will be s and it will point it. So far everything is fine. As soon as I write the second line, S is equal to world. Obviously, world is also written in a literal way. So, where will this be in our string pool? JVM will see if there is a string named world that doesn't answer. So, we will make a new object. I will make it white. So, we made a new object. Its value is world. S points to this world and breaks its connection with this old string. Something like this. Now, who is pointing this hello? Nobody. Now, whenever our garbage collection runs, and no one is pointing this hello, then it will kill it with memory. Right? It will remain in memory for some time. But, who is S pointing to? Our world. Now, if you print it, system.order println, then your answer should be world. Right? Okay. Now, if you have cleared all these things, then there is one last question, which is very interesting. Now, if I write, let's say, string s. And this time I will use a new operator, new string So as soon as I use a new operator, you should understand that this is going to go into our heap memory And let's say I pass hello in the string Something like this So what will happen? Now we have understood many times that this is our heap memory In this, suppose the upper part is our string pull and the lower part is the normal heap And let's say this is our stack memory So as soon as I write new string hello, an object named hello will be created in our normal heap and a reference variable named s will be there in our stack and it will point to this. We know this, but remember we just talked about a rule that whenever we see a literal, whenever Java sees a literal, right? it will quickly put it in the string pool. Now, here many kids get confused. Let's see, if we have written a new string, then where should that string go? In the heap memory? This is absolutely correct. But because you will pass a string in this constructor, and that string will be a literal. So, as soon as Java will see this literal, it will pick it up and put this hello in our stack memory too. Yes, obviously, no one will be pointing this hello right now, that's a different matter. If you declare a new variable tomorrow, s2, and write it as string s2 equal to hello. So what did you use? You used the literal method, right? You didn't use any new operator and you wrote hello. So now what will happen? A reference variable named s2 will be created. JVM will check if there is already a string named hello in our string pool. The answer is yes, there is. So s2 will directly point this. We didn't have to make a new hello because it was made when we made a hello for heap memory Because to make a hello in heap memory, we passed hello in constructor But as soon as we passed hello, how did we pass it? In a literal way And you know whenever we make a literal, where does that literal come from? In our java's pool, in our string pool So if you understood all this, then let's quickly go to code and see all this So here again I have made a file called demo2. java And we will first see our first law. So let's say I took string s1 and put JA plus VA. And I took a string s2 and put JAVA directly in it. JAVA was the first capital. And I asked you what if I write system.out.println and I try to print that is s1 equal to s2? And we saw that it is expected that our answer should be true. Why should it be true? Because both of these are literals, so they should be resolved at the time of compiling. And what should it become after combining? It should become Java. And this java will be stored in our string pool and s1 will point it Then when we will write the next line s2 is equal to java So JVM will check that the string named java already exists in the string pool If the answer is yes then s2 will also point it Now both s1 and s2 will be pointing the same java string So the answer of s1 is equal to s2 is true So this is our expected Now we have run it and the answer is true. So this part is clear. Now let's move on to the second part. Before moving on to the second part, let me comment on this. So now let's do one thing, let's take another string, let's say s3. And again I put java in it. And let's take another string, let's say s4. And let's change it from java to hello. So I put hello in it. And in s4, whatever is inside s3, Plus, world. So I didn't write world directly, I gave space and then world so that there is a gap between hello and world. So what I am saying is, put hello in S3 and whatever is in S3 plus world in S4. So you know what will happen? This hello will go to our string pool. Now world will also go to the string pool because it is a liter. But when will S3 plus world resolve? It will resolve at runtime. So what is hello in S3? So hello world will go to our heap memory. So I keep writing one hello world, let's say we write it here. So let's see what's in the string pool. So the first thing that comes in the string pool is hello. So here I wrote hello. Okay. After that, as soon as this line is executed, you must have seen that a string named world is also literal which exists. So I write this space world. This will also come in our string pool. So a hello and a world came. Then I wrote s4 is equal to s3 plus world. Now s3 plus world is resolved on runtime. So whatever is resolved on runtime goes to our normal heap memory. So hello and world. Where did our hello world come by combining? Now we are in the heap memory, something like this. So hello and world are in the string pool and hello world is in the heap memory. Now let's say I take another string, s5, and I put the whole hello world in it. Something like this. So I put the one here in here as well. So now what is in our string pool? Our hello world is here. Something like this. So now if I write system.out.println and I ask you that is S3 sorry not S3 S4 is equal to S5. Our expected answer is false. Why is it false? Because look, in S4, although there will be a hello world, and there is a hello world in S5, but the hello world of S4 is in our heap memory, and the hello world of S5 is in our string pool. And both are different references, so the answer of S4 is equal to S5. This is false. We have also seen why this one is in our heap memory. Why? Because it will be resolved dynamically. It will be resolved on runtime. Because a variable is written here. And this is because it is a literal. So it will go straight to our string pool. Okay. So let's see its output once. The answer is false. This is what we expected. So this was our second case that we saw. Now let's move on to our third case. So for the third case, I have already commented on it. Now I will take a new string. So from 1 to S5, let's take S6. So let's say I put hello in this. And let's take another string s7 And let's put whatever is inside s6 Something like this Now if I ask system.out.println Is whatever is in s6 equal to s7? Right, so I have put equal operator here. It compares references. Now again, you know that this assignment here, simple equal assignments, it resolves in our compile time. So if it resolves in compile time, then hello will also come in S7. Now because hello is already there in S6, it will go in our string pool. Now S7 is also saying that I have to save hello. So there is already hello in string pool, so S7 will also point it. So again S6 and S7 both will be pointing hello. So its answer should be true. So let's see this once. So we are approving the same cases that we talked about on our iPad So now let's see our next case In which I told you that let's say I have a string s8 And let's say it stores hello And then again I put wild in this s8 And now if I do system.out.println Our S8 So you know what should be the answer? The answer should be the world Right? So let's see this too The world came. Right? So what happened? First asset said that I have to store hello. So where will hello go? In our string pool. So asset will be pointing hello. Then asset said that I have to point world. So in our string pool, a new variable will be created. A new object will be created. World. And asset will start pointing to it. But yes. Hello will still be present in our string pool. But no one will be referring to it. If no one will refer to it for a long time. And garbage collection runs. Then hello will be killed out of our memory. Okay. So we talked about this too. Now, let's see our last case. And that case is, let's say I take a string S9 and create it using new operator, so new string and here I write hello. Okay, now I have saved both hello, but I have made one by using new operator, that means it will go directly to our heap memory, and I have made one by using literal, and it will go to our string pool, but here is catch, which I had told you, now as soon as first line will execute, s9 equal to new string hello, so obviously because we have new operator, so our one string will be made in heap memory, in which hello will be present, that is absolutely correct, but because here we have passed hello as a literal in constructor, so this hello which is passed in literal, This will also run in our string pool. So when these two lines will be executed, there will be a case that our string pool will also have hello in it. And our heap memory will also have hello in it. Something like this. So now you know that if I will try to do system.out.println, is s9 equal to equal? S10K, then obviously the answer will be our false. Why? Because one is in our heap memory and one is in our string pool. Both are different references. So if I run this, then the answer will be our false, which was also expected. Right? So now if you understood that well, then I have a very interesting thing for you, which we call problem of immutability. Okay? Now what is this immutability problem? Let's see this quickly. So let's say I write a code here. First of all, I took an empty string, string s equal to empty. Now, obviously, if I declare it as string literal, then where will it go? In our string pool. So then I start a loop, let's say 5 times i equal to 0 to i less than 5, i plus plus. And what do I do every time? Every time I do s plus equal to, let's say, i. Something like this. And let's do one more thing. Every time we print this string too. Whatever our string will become. Okay, s. So what do you think will be the output? If we try to dry run it, then first of all, our string will be empty in S, then our loop will run from 0 to 5, so first of all, it will run 0 times, we will do S plus equal to I. So as soon as we did S plus equal to I, what does S plus equal to I mean? S equal to S plus I, something like this, right? Now in I, the store is 0 and what was originally stored in S? It was an empty string. So if we do empty string plus 0, it will be concatenated and 0 will come in our S. So it should be printed on the screen for the first time. 0, something like this. Let's write 0 in the form of a string. So the first time it was printed as 0. Then our loop will run. This time the loop will run again. This time the value of i will be 1. So we will do it again. Whatever is in S, add i to it. In S, there is 0. In that, we will add i to it, which means 1. So next time our print will be 0, 1. And then this loop will keep running like this. Next time our print should be 0, 1, 2. Then our print should be 0, 1, 2, 3. And then our print should be 0, 1, 2, 3, 4. Now as it tries to run in 5, our loop will come out because i can run till 5 So our 0, 0, 1, 0, 1, 2, 0, 1, 2, 3 and 0, 1, 2, 3, 4 These 5 strings should be printed So let's run it once Absolutely, these 5 strings are being printed which we expected Now I wanted to talk about what is the problem of immutability So let's run it on the iPad So look, here I wrote the same code which we had written in our compiler I told you that because the strings in Java are immutable. What does immutable mean? That the original string never changes. So, we just saw that if I write string s as hello, something like this, and I write s again, whatever is in s, plus whatever is in s, plus add world to it. Right? Something like this. So, we are expecting that our string, whenever I do system.out.println s, then the answer should be hello world which will also come, right? Why will it come? Because I am saving it in S only. What am I saving in S again? Whatever was in S earlier, which is hello, plus put world in it, right? And we also know how it works, that in our heap memory, our string pool part, hello will go there, okay? And its reference pointer S1 will come here. And now because I have written next S1 plus world, so now because this is solved on run time, not on compile time, so where will our hello world come? In our normal heap, hello world And here we will point S1 from here and remove it from here Now this S1 will be pointing here In the normal heap memory We know all this But we also know that our strings are immutable Which means whenever we change something in S1 S1 points to a new variable. Right? So in this case also, if S1 was pointing to hello earlier, whether it was doing it in string pool or heap, as soon as I wanted to do that hello to hello world, my compiler said, no, I will make a different object with the name hello world. I will not change in this old object. What will I do? The reference of this old object, I will change it to this new object. Something like this. And this hello will remain in my memory. Now you know which string will be in the pool and which will be in the heap. But we are not going to that now. But whatever it is, both will be in our heap. Because string pool is also a heap part. So because our string is immutable, we will create a new object every time. In this case, see what will happen. If I remove this. In this case also, what happened first? First of all, as soon as I wrote this line, s is equal to empty string, our heap memory will have an empty string in it. Something like this. And our stack memory, which has the variable s, will be pointing this empty string. Then we run the loop 5 times. As we ran the loop for the first time, we said that whatever is in S, put it in plus I. Now you know that this will be your run time resolve. So if this upper part is our string pool, then the lower part is our normal heap. So we call this string pool and this is our normal heap. Since the lower part is a normal heap, it will resolve as s plus i, which is empty string plus 0. So, string will be 0 and it will be stored in the normal heap. So, let's say a new object is created and it stores 0. Now, s will point this and not this. Something like this. Right? I'll remove it. Okay. Then again this loop will run. This time the value of i will be 1. And we'll do whatever is in S plus i. That means a new memory will be created in the heap. Which will store 0, 1. 0, 1. Something like this. And S will now point to this. Now it will not be pointed like this. Then it will be 0, 1, 2. Now it will be pointed like this and not like this. And this thing will continue to work. Until we exit from this loop, the values will increase in the heap. So first it will be 0, then 0, 1, then 0, 1, 2, then 0, 1, 2, 3, then 0, 1, 2, 3, 4. Something like this. all this will come in our heap and in our string pool there is an empty string so this will be the complete scenario so think to run a loop we have declared so many objects in memory which have no use because finally s must be pointing to this Our final object, 0, 1, 2, 3, 4 So all the things we made, they are still the same, they don't have any point Now we will wait for garbage collection to come And remove all these from memory And also the empty string above, which is in our normal string pool Now look, why did Java make strings immutable? Because it wanted strings not to change, right? Because we use strings in a lot of places, we don't want them to change. But with making strings immutable, some problems also came. And that is the problem that we discussed, that our memory is wasted a lot. Okay? So, Java has brought a solution for all of these, which is called StringBuilder and StringBuffer. These are two different classes that solve the problem of immutability of Java. This StringBuilder and StringBuffer are not immutable, they are mutable. You can edit them, insert them, you can do whatever operations you can think with the string in this. New objects are not created every time. But before moving there, let's first understand the String class and then we will move on to them. So now let's look at the ins and outs of our string class. So if you look at your string class from the inside, it will look like this conceptually. That will be your public final class and its name will be string. Something like this. This is conceptual representation, there will be a lot of things inside But if we look at it, what will be big? It will be our final class because we know that it is an immutable class and its name will be string Now what things will be inside it? That is very important We discussed that what is string? After all, it is a wrapper on the character array So there must be a character array in it That means we have a private final Why final? Because you know that we can't change it, it is immutable private final will be a character array. Let's say its name will be values. Something like this. Because we know that whatever I store in string. Let's say I stored Aditya. So from inside it will be stored in a character array. A, D, I, T, Y and A. Something like this. This was absolutely true before Java 9. After Java 9, we completely ditched this character array. Java said that it consumes a lot of memory. Instead of this character array, we should use the byte array. So basically, what do we prefer here? Now we prefer the byte array. Now do you know what is byte? Byte is a type of int, right? The one that takes the least memory. A byte takes one byte, right? And what is this? The smallest part of an integer. First comes your byte, then comes your short, then comes your int, and then comes your long. Right? So this is the same byte. So he said, let's take an array of bytes, values. I will store things in that rather than taking an array of characters. Now we will see how this optimization happened later. Apart from this, we have another field, which is private final. int coder and hash private int hash Now, what is this hash? It is nothing. Basically, we will store the value of the hash code of a string in this hash. Because you know what a string is. It is immutable. So, whenever a hash code of a string comes out, it will come out there. So, we want to store the hash code of a string here so that we don't have to take it out again and again. Basically, if we have a string, let's say S, and I write S dot hash code. So you know a hash code comes out and this hash code comes from our object class, we already know this. So whatever hash code comes out, we will save it in this hash so that we do not have to remove hash again and again. So, this is how our conceptual overview of our string class looks like. Right now, we don't know what are the values, coder and hash. Now, let's discuss about it one by one. So, first of all, let's see that what does our byte of values actually store? See, it stores exactly the string in which we have put the value. What does it mean to say? So, let's say we took our string, string s equal to java. Something like this. So you know what will happen? Our java will go into the string pool and the s reference variable will be in the stack and it will point it. Now the byte's array of values will store the unicode values of all the characters of j, a, v and a in an array. So that means we will have an array of values from inside something like this and it will have 4 fields because we have 4 characters. A, V and A. And we will store the Unicode value of these four in this value. So, I will write their Unicode value. So I have written the Unicode values of these 4 as 74, 97, 118, and 97 So the question is that we have taken the byte array values from inside We had to store Java, what does the string do from inside? It stores the Unicode representation of all these So it stores 74, 97, 118, and 97 like this And when we go to the memory, you know that 74 will not be stored like this This will be stored in bytes. 97 will also be stored in bytes. Like this and this too. Everything will be stored in the form of zeros and ones. So we understood how to store this. But tell me one thing. Why did Javan think that by not taking the character array, I take the byte array? Wasn't the character array simple? By not storing it simply like this, I would have stored it like this. J, A, V and A. Something like this. Maybe you think that we have to store in numbers only. In zeros and ones. So what happened? Numbers will also be of these. Whatever number j is, 74, I will write it in binary in memory. Whatever number a is, 97, I will write it in binary in memory. Right? I am not getting any benefit from this. You know, whenever j is stored in the block of memory, how does it get stored? First, we get the value of j, Unicode value, which is 74, and the binary of 74 is stored here. So, Java will store it in the binary anyway. Why am I not taking the character array to make my work easier? Which I used to take before Java 9. Before Java 9, I used to store it like this, j, a, v, a. But why did I think that instead of storing it as a character array, I will store it as a byte of array. This is very interesting. If you understand this, then you will also understand what a byte coder is. So look, we understand this problem very well, which is very interesting. So what used to happen earlier, whenever we used to write string s equal to java, I am talking about before Java 9 because after that we shifted to byte array So what we used to do from inside, we used to make an array of characters So we used to make an array of characters, values and we used to put all these characters in it So here comes J, A, V and A something like this But look, we know how many bytes a character takes in Java 2 bytes, we know that in java a character takes 2 bytes so here j will also take 2 bytes a will also take 2 bytes basically every section of this will consume 2 bytes similarly v and a will also take 2 bytes so in total we have 8 bytes So we have added 8 bytes to store this Java string. But if you think about it carefully, all the values in this array, j, a, v, a, all of these are the values of ask. What does this mean? We know that that small a to small z, big a to big z, 0 to 9, and all our special symbols in the keyboard, dot, comma, equal to, etc. We can represent all these in the values of ask, which is from 0 to 255. So we can represent this in 1 byte And 1 byte means 8 bits And how many possible combinations can there be in 8 bits? 2 to the power of 8 which is 256 Which means from 0 to 255 We already know all this So now if we already know These are all ASCII values and ASCII value comes easily within 1 byte. So why are we taking 2 bytes to store every character? Because our character in Java is of 2 bytes. But why is the character in Java of 2 bytes? Because in Java we use characters to represent the whole Unicode. Right? And in Unicode, we don't just get these values. All the characters in this world, whether they are Hindi characters, Chinese characters, any language character in the world, they all come. Right? And when we go to represent all of them, then there are some characters like Hindi characters. So let's say we take a Hindi character, let's say Ka. Okay? So this Ka, which is a Hindi character, we need 2 bytes to store that. Right? Because this is a part of Unicode, not a part of ASCII. To store ASCII, we need only 1 byte. But to store Unicode, we need 2 bytes. And what all digits we keep in those 2 bytes doesn't matter. Right? Like we For small a, we keep 97, which comes in one byte. For this curr, we keep two numbers. For example, let's say something will be 23, something will be, let's say, 98. Anything can be, those two numbers. Okay? We don't care about that. But we came to know that outside of this ASCII limit, we need two bytes to store all the symbols. For example, we need two bytes to store this Hindi character curr. Okay? That's why Java made a rule that we will keep every character in two bytes. But think about it. Generally, when you use strings in a program, you are storing the values of the ask. It's very rare that you use Hindi characters, Chinese characters, or emojis. So Java thought that if this is the case, Most of the time, people are using English characters or numbers or whatever you enter as a keyboard. So in that case, why should I store 2 bytes and consume memory? Because in the earlier times, before Java 9, we used to consume 2 bytes for all of them and it used to be 8 bytes. So Java said, let's do one thing. From inside, all these get ASCII numbers. Small a to small z, big a to big z, 0 to 9 and all these are special symbols. And all these come in one byte. So why should I take an array of one byte? So basically, if someone writes on the screen that string s equal to let's say java. So I will not make a character array but a byte array. So I will make a byte array. Obviously it will be of 4 lengths. Because j will be 1, a will be 1, v will be 1 and a will be 1. And do you know how many bytes does this byte take? Obviously it takes 1 byte. Our data type byte takes 1 byte. So every block in it is of 1 byte. Now we will write the value of j here, a here, v here and a here. And what will they take? They will consume 1 byte each. So I will write all of them. As soon as I used their ASCII values, how much memory did I consume? I was looking for 8 bytes to store this. Now I am looking for 4 bytes. So Java chose to use the byte array after Java 9. Before that, it used the character array. Because it used to store everything in two bytes. Every character in two bytes. Right? But we know that most of the strings that are made in Java are the values of ask. And you can represent the values of ask in one byte. Because its limit is 0 to 55. And what comes in it? All the English alphabets, all the digits and all the special symbols. Right? So if we can represent all of them in one byte, then why don't we make an array of bytes only? Because if we make an array of characters, then again every block will take only two bytes. So, we make a byte array because byte takes only one byte. So, here I have kept their numbers. Which numbers? The values of all their ask. If we add the values of all their ask, then it will take 4 bytes. So, I reduced my space by 50%. Very good. But yes, obviously, there can be many such cases in which we are actually storing the character of some other language in our string s. Let's say, I will write k. I am storing characters like this, then what will happen? Now I can't store this in each byte. Now, it will take two bytes for me. And it will take two bytes for me. So, in total, it should take four bytes. Right? So, how do I get this to work here? There I use another field of Java, which is in the string class, which is coder. Now, what is this coder field? See, this is our next field, byte coder. It can have only two values, either 0 or 1. When will its value be 0? Its value will be 0 when you are going to put those values in a string which all come under the ASCII characters. Right? So in that case, it will be 0. But if you are going to put special characters in a string which go beyond the ASCII limit, which basically come under the Unicode limit, in that case, its value will be 1. So basically, the coder tells us how the formatting of our string is. If it comes to the ASCII limit, then its value will be 0 and if it doesn't, then its value will be 1. Now, when its value is 0, that is, when it comes to the ASCII limit, we call it Latin format. Exactly, we call it Latin 1 format. And if it doesn't come to the ASCII limit, then we call it UTF-16 format. Now, you know, this one takes only one byte. Latin format, which is within the ASCII limit, and UTF-16, which is outside the ASCII limit, which is within the Unicode limit, takes 2 bytes. Okay? So, with this variable, a variable called byte coder, our string class decides that whatever we are going to store in the string, let's say, Aditya, Can every character be represented in a value of ask? If possible, then the value will be 0. And if not, then the value will be 1. Now what does 0 mean? We have to read only one byte every time from this byte array. Why? Because if you read one byte, you will get a character. But if the value of the coder is 1, then we have to read 2 bytes every time from this byte array. Because only then will we get a character. So let's say when I first stored Java in S, So our values array will contain all the ASCII values. And our coder will get zero. Why zero? Because all these are in Latin format. All these are ASCII values. J, A, V, A can be stored in ASCII. So a coder will tell you that whenever you read this string, you have to read only one byte at a time. So when I read it, I will read 1 byte, 74, I know 74 means J, then I will read 97, it means A, then 118, it means V, and then again 97, again A. So I have read Java. Okay. Now look at the second case. Now in the second case, let's say I have stored K and G in the string. Now I'll take the Hindi character which takes 2 bytes every time. So what should be the length of the values array? It should also be length 4. Why 4? Because every character will take 2 bytes this time. So I need 2 bytes to represent Ker and I need 2 bytes to represent Ger. Now we can see the 2 bytes of Ker in Unicode table. We can see the 2 bytes of Ger in Unicode table. That doesn't matter. What matters is that we need 2 bytes to represent Ker. and we need 2 bytes to represent this and in that case our coder variable's value will be 1 so the value of coder 1 will tell our JVM that whenever you read this values array then read 2 characters at a time that means first of all you read 2 characters then you see that this is a and then you read 2 characters and then you see that this is a so my entire string is like this Here we were reading one character at a time. Here we are reading two characters at a time. Here the value of this coder comes. I hope you have understood well what optimization Java did. So what Java used to do before 9, it used to directly store the character array. So it will literally store J, A, V, A like this. Disadvantage is that it takes a lot of memory. Here it is taking 8 bytes. But we know that most of the time, in fact 95% of the time, we always store the values of today. So to optimize that, what did we start using internally? The byte array. Because byte takes only one byte at a time. Now, yes, there can be a special case when we need 2 bytes. In that case, we will read 2 bytes at a time. We will keep a new variable, coder, which will tell us whether we have to read 1 byte or 2 bytes. Its value can be either 0 or 1. It depends on whether it is a Latin format or UTF-16 format. That is, is it ASCII format or Unicode format? Now, obviously, the last thing left in our string class was our hash. Something like this. And we discussed what is hash. Actually we know that we can take hash of everything in java. We know that we just take a string s and if we write hash code on it, s.hashcode, then we will get an integer value which will be its hash code. Now we can store this hash code in this hash name variable. So let's say we have a string java, something like this. As soon as we write s equal to java, all the values of this byte will be stored and the coder variable will be zero. Do you know why this will be zero? Because all the values are here. As soon as we write s dot hash code, whether we write it ourselves or use something like hash table or hash map, or we used the hash set, which we will see later in the collection framework. But if we used a hashing function from inside, then it will remove the hash of the string from inside. Now whatever hash comes, let's suppose, as we wrote this s.hashcode, its hashcode comes 1, 2, 3, anything. Now we will store this 123 in this hash name variable And why will we store it? Now it will work as a cache Now whenever we have to remove the hash of s So whenever we call s.hashcode So it will first check whether its value is present in our hash variable If it is present then it will directly give you 123 Right? Every time we don't have to remove its hashcode by doing s.hashcode Because if you remember we saw in object's lecture How the hash code function looks like So obviously it is a bit time consuming We take prime numbers and multiply it with big prime etc etc We know that the hash code procedure is a bit expensive So why do we do it again and again We can directly put it in the hash and do the cache Once we put it in hash, whenever we need it, we will say that string has been checked in its hash and the hash code is already saved here. If it is saved, then we will mix it and give it. If not, then we will remove it again. Now see, the most interesting thing is that we can keep this hash variable in this string class because our string is immutable. If our string is not immutable, then this string can be changed and if the string can be changed, then its hash can also be changed. But because our string is immutable, it cannot be changed, so its hash will never change, that is, our hash will always remain the same. So our string class becomes very optimized in all these ways. So I hope you understood well that what all optimizations Java has put in strings, how it shifted from character array to byte array, why it kept a variable named coder, why it kept hash, and we also saw what a string pool is, how we declare strings from a new string operator, and further optimizations of strings. So now if I talk about the optimizations of a string, how Java has optimized the string class, then you will understand that we generally talk about two optimizations. One is that Java used the string pool, due to which many of our new objects became less, because we know that we can reuse strings a lot. Second, Java shifted the character array By the way, the size of our string has reduced by 50%. Earlier, the string took 8 bytes, now it is taking only 4 bytes. And there is one more, the third one. And that is, caching the hash component. Right? We cache the hash values. Once we get the hash, we store it in the hash name variable. And then whenever we need its hash, we can ask it. So, for these three reasons, Java's strings are very optimized. Working with it is very easy. And obviously, you know that Java's string is immutable. This property is its most important. Okay? Now, this video is already very long. Now, we will discuss the methods of string, what is string builder and what is string buffer in the next video. I hope you enjoyed it. You got to learn a lot of new things. That's all for this video. See you in the next video. Till then, thank you so much.