Java Strings are Immutable - Here's What That Actually Means

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you've probably heard the concept that strings are immutable in java which means they can't be changed but you may be thinking hey i've changed a string object before all you have to do is take your variable and set it to something else yes you can do that but that's not what string immutability means in this video we'll talk about what strings being immutable actually does mean all the reasons why they were made to be immutable and what it means to your programs my name is john i'm a lead java software engineer and i love sharing what i've learned in a clear understandable way so if you like this video consider subscribing so you don't miss each new java tutorial i also have a full java course available in a link down in the description if you're interested so go check it out all right so we know that string objects are immutable and they can't be changed but you know that you can declare some string and set it to some initial value and if you want you know you can set it to some other value right so what's the deal there to answer that question it's important to know what actually happens when you create a string object so in our example we create this name variable and assign it the value john what java does is create that string variable called name but that name variable isn't the string object itself what it is is a reference to a string object in memory that it creates with the value of john but when you take this name variable and assign it a new value java doesn't actually modify this string object in memory what it does is create a brand new string object in memory with the value larry and changes this name variable to point to that string object instead and it no longer points to that john string so when we say that string objects are immutable we're talking about the string object in memory the string variables themselves can be changed to point to whatever string we want it's the string objects themselves that are not changeable they're immutable so that's cool and all right but who cares why does it matter why do we not want to be able to change these string objects there's actually a few great reasons the first reason is that it enables java to save a ton of memory space so let's say we had this string name equals john then we also had string another name and it also equals john these are of course the exact same string literals right when you create multiple string variables and set them all to the same literal string values java does something pretty smart so of course java creates another variable another name flawless handwriting and you might think that java is going to go ahead and create a whole new object with the value of john and it's just going to point to that right well it actually doesn't do that when java creates a string object from a literal it actually puts that string object in something called the string pool and then every time another string literal is created java will check that string pool to see if that value is already anywhere in there so over here when we say another name equals the literal string value john java goes okay do i have that john anywhere in the pool already ah yes i do and it takes that another name string variable and points it to the exact same string object that it already created before that's pretty cool right it's using half the memory that it would if it were to just create a brand new string object each time but what does that have to do with strings being immutable well the string objects weren't immutable this wouldn't work at all if you had both of these string variables pointing to the exact same string object in memory if this name variable was able to change this string object in memory like instead of saying john it said carl that would also change the value of the string being referenced by our another name variable so this whole memory saving scheme using the string pool wouldn't even be possible if strings were changeable but because strings are immutable we don't have to worry about any of that we can have 2 3 10 a million different variables all with the exact same string value and pointing to the same string object in memory and we know that none of them are going to be disrupted because that string object and memory can never be changed there is something to keep in mind there though so java will do that automatically with string literals like this it'll automatically use that string pool but if instead you go ahead and create another string variable a third name and instead of just assigning it to a string literal you use the new keyword and say new string john so java will go ahead and create that third name variable but because we use the new keyword it won't use this shared object in the string pool instead it'll go ahead and create a brand new object outside of the string pool with the value of john even though it has the exact same value in it java will go and create a whole separate object for it and there's a way we can prove that too if we print out the value of name double equals another name java's double equals will always return true if both of these variables are referring to the exact same object in memory and of course we know that our name and another name variables should both be pointing to the exact same john string in the string pool and if we go ahead and run our program yes indeed it prints out true but if instead we compared name with a third name it prints out false and that's because the name and third name variables are pointing to separate strings in memory they're not pointing to the same objects the second reason that java has chosen to have immutable strings is for security so let's say we had uh some method public void add money to account and that takes in a string account holder and like an int or money to add you know for like a simple banking system this could be a method that just adds some money to somebody's account so if you were to write this method you might have some like validations to make sure this is an okay transaction to make and then you have a bunch of code to actually add the money to the account but if string objects could be changed you have a potential security flaw here this method will be called from somewhere else in the code right so i can call add money to account and pass in the name string which should be referring to the value john and some amount of money like uh five thousand dollars and we do need to go ahead and make this method static so we can call it from here so we've got a situation like this where this name variable is pointing to the string object in memory with the value john and when it's passed into that method there's another variable called account holder and that's going to be pointing to that exact same string object in memory but the code here that's calling this method still has access to this name variable that is pointing to the string object in memory right so if java allowed changing that string object in memory you could write some really mean nefarious code right here that would try to wait until these validations are performed in this method and then it can go and use this name reference to this object in memory and it could just change it to something completely different like carl it just did all its validations on the account holder being john but then when it goes to actually add the money to the account it adds it to carl's account instead but that's not the case in java because strings are immutable it doesn't matter that this name variable still has a reference to that same shared object in memory this method knows that the string object that this account holder variable is pointing to is never going to change with strings being immutable that security risk just goes away that leads to the third benefit of immutable strings which is that strings are completely thread safe so in your java program you could have dozens or hundreds or thousands of threads all pointing to the exact same string object in memory and all of them can be reading that value from memory whenever they want even though all those threads are using it none of them are able to change it so they can all use it completely safely if you learned a little something in this video let me know by hitting the like button and as always if you really want to support the channel you can do the whole youtube trifecta of leaving a like a comment and hitting the subscribe button and if i got something wrong here feel free to yell and scream at me in the comments and be sure to check out all my other java videos as well i know you'll find a ton of stuff you'll like thank you so much for watching i'll see you next time
Info
Channel: Coding with John
Views: 512,519
Rating: undefined out of 5
Keywords: java, codingwithjohn, coding with john, java beginner lesson, java strings immutable, java immutable strings, java tutorial, java immutable, java immutable class, java immutable variable, java immutable string, java Strings, string, java string, java programming, immutable string, string class in java, programming, why string is immutable in java, why java strings are immutable, why string is immutable in java interview questions, why strings are immutable in java
Id: Bj9Mx_Lx3q4
Channel Id: undefined
Length: 7min 6sec (426 seconds)
Published: Mon Sep 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.