Java Strings are Immutable - Here's What That Actually Means
Vložit
- čas přidán 26. 06. 2024
- Java String are Immutable - but what does that really mean?
We'll learn why Strings are immutable in Java, and what it means for your Java programs.
String immutability in Java means that String objects cannot be modified after they're created. Let's talk about why it matters that Strings are immutable, and why Java chose to make them immutable in this beginner video lesson tutorial.
Learn or improve your Java by watching it being coded live!
Hi, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java learners.
Let me know what else you'd like to see!
Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
📕 THE best book to learn Java, Effective Java by Joshua Bloch
amzn.to/36AfdUu
📕 One of my favorite programming books, Clean Code by Robert Martin
amzn.to/3GTPVhf
🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
www.audibletrial.com/johnclean...
🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
bit.ly/3QPNGko
📹Phone I use for recording:
amzn.to/3HepYJu
🎙️Microphone I use (classy, I know):
amzn.to/3AYGdbz
Donate with PayPal (Thank you so much!)
www.paypal.com/donate/?hosted...
☕Complete Java course:
codingwithjohn.thinkific.com/...
codingwithjohn.com
Hey John! I do not leave comments often, so this is a special event. I am currently learning Java at university and at home, and I wanted to tell you how much your videos are helping me. Your simple way of teaching is amazing for new learners, and I find your channel criminally underrated. Keep on uploading, Im sure youll get big on CZcams, a 100%!
Thanks a ton! I'll keep working on it, glad it's helping you!
Was going to say the same, usually programming videos are either boring or not engaging and taught well. This is completely opposite, very clear and understandable. Thank you
The best creators bubble to the top!
@@CodingWithJohn Same here. I have a Java exam in June and your videos are a good revision for me. Many thanks.
Recommend if you're serious about mastering Java that you read e.g. Joshua Bloch rather than watching needlessly long youtube presentations. No criticism of this video intended.
Plus one interesting thing - due to String's immutability and presense of a string pool - the hashCode() method is calculated only the first time and saved as a value inside String object. Therefore when you call hashCode() method second time (e.g. on another variable with the same literal value) - it will just return stored value, no need to calculate hash code again.
ґалічєнин
Yura, thank you for sharing this interesting fact, the video itself and the comments are a real treasure for a Java learner!
👍
Really cool!
Thanks a lot for the crisp and clear video, John. Just to summarize String Immutability Benefits -
1) Usage of String pool, saving tons of memory and hashCode calculation
2) safe for multithreading
3) Removes any security threat by not allowing hackers to change referenced string values to cause security issues in the application
You basically summarized the summary.
Thanks for summary. Question regarding #1: Do I understand this correctly: the String pool saves the memory ONLY if you have two or more identical string variables (they point to the same piece of memory), but it wastes memory EVERY time you are changing the strings (it uses new piece of memory for the whole new string even if you change only one letter)?
I always had a question about this topic. I read in a book about java, that strings were immutable, but it never went so much into detail. This is a very good video. Thanks for sharing.
first time in this channel and it's absolutely great to dedicate a specific video to talk only about immutable objects in less than 10 minutes, perfectly clear!
Dude I’m in bootcamp now for 6 months and no coding background. I just watched your video and I can say you have a best teaching technique that super understandable. You just got a subscriber. Thanks.
Thank you! I will for sure be checking out more of your videos. You explained everything very clearly!
You have a talent for explaining things in a concise and easy to process manner. Thank you, sir!
Outstanding and easy to understand explanation! Thank you for this video!
Man I just discovered your channel and I find your content amazing. This is the kind of depth I was looking for regarding string immutability.
Man, you are so amazing. I've read many guides about immutable string but none of them is as clear as your explanation
You are easy to understand and always to the point. Glad I found you. Keep up the good work. Cheers.
Such a simple topic explained so well, wish they explained it like this in my early CS courses
I love how you’re drawing out with examples as you explain
Ive been using Java as a student for nearly 5 years now, and your content has been completely mind-blowing to me. Made me adopt the "I know nothing" attitude and relearn everything.
I think you're kinda missing the main reason why Strings are immutable: You can't really have sensible mutable Strings since you can't change their length without moving everything to newly allocated memory on the heap which is incredibly expensive.
I absolutely see your points as benefits, in fact I think Rust has proven that the whole mutable by default approach is a mistake, but it might have been a good idea to go into the actual reason a bit.
I can think of multiple ways that you can change your String length during runtime without coping (at least most of the time). Lists, Allocating more than cou need and and only get new memory when you run out.
@@XD-nj7bc allocating more than you need doesn't really solve the problem though, as you'd then just use more than you need, making it more expensive than immutable Strings
@@watertrainer3992 of course it is not cheaper than not mutable strings but you can then change the size without the copying he mentionend.
@@XD-nj7bc I put the word "sensible" in there for a reason 🙂
@@XD-nj7bc yeah he put the word "sensible" in there for a reason bruh
Knew all this but still his way of explaining held me till the end.... His way of teaching is just amazing
Crystal clear with examples. Hats off to you.
Thank you for taking the time to make this video. I have liked, subscribed and am commenting. Cheers!
simple, clear and straightforward explanation. thanks for sharing
Now Everything has sence. I know many teachers that needs THIS VIDEOS. You Rocks John.
To the point , Every bit of information provided in just 7 minutes. SUBSCRIBED !!!
Wow, wow, wow, FINALLY the explanation of immutable strings that l was looking for sooo long time! THANK YOU!
There was an error i was dealing with ages ago that i couldn't figure out and this video made that error make perfect sense, thank you!
Simple and clear explanation John. I appreciate your work and effort. Amazing stuff!
Great explanation! Easy to follow, clear and concise.
You earned a new subscriber!
I think I'll never look back again at why string is immutable, you made it super clear 👌🙏 awesome!!!!
Thanks so much. I've just come up with an interview question about this subject this morning. Luckily I watched your video the night before :)
Again, thank you for explaining concepts better than most professors
Thank you so much for explaining this!! I never truly got a grasp on what was meant by Strings being immutable but you explained it perfectly!!
I'm self teaching Java currently, I have a lot of questions about the "whys" and "hows" but currently the JavaDocs are a bit too advanced for me. This really helped!
@@jake9854 which dark age r u living in ?
Thank you John.. That was the best explanation for understanding what immutable means
Wow, it's great video! Informative, comprehensive and not boring.
Thank you very much!
Excellent lesson John! I'm learning a lot with you :)
John, I love your videos. Thank you. Studying for the OCA at the moment and you explain these concepts so well that I do not need to study them after watching the video. I understand it, which means I remember it forever because I understand how it works. I really appreciate your help!
You simplify it in an amazing way well done :)
This was awesome. Thanks, John!
Good, quick description 👍
Simple Concise and to the point.
This was fantastic, thank you so much! I've been struggling a lot in my comp sci classes because the professor refuses to go over theory, so this is wonderful :) You've won a very very faithful subscriber haha.
you are the best java teacher that i saw on the youtube because you are cleary explain what is the behind theory thank you very much 🤩💖
I am currently learning Java your videos give deeper understanding of Java thank you very much!
Your an awesome teacher! I remember studying this topic in my book and could not understand it till now. Thanks.
Love your concise, clear, and informative videos!
I normally try to read Oracle Tutorial Trails, but these videos are faster on certain topics. Plus you include great examples.
Thank you!
Those are good too (as they should be, coming directly from Oracle) and I've used them to do some research on various topics I've done videos for. But yeah, often it's easier to watch a video on it. Thanks!
@@CodingWithJohn thanks for the reply! I’m a Comp Sci graduate who just finished my first year working a software job.
Your videos are amazing!!You really do make the concepts clear!!Thanks for such an amazing content.
Kudos to you for explaining in such a short time. Cheers man
I don't have a words John sir, you are a real teacher I've ever seen , iam very lucky becouse of i found your vedio ☺️☺️
What a great explanation! Thanks John!
This is the best explanation of Immutability on youtube
You are a gem 💎
Thanks man!! Totally understood this concept :)
Crystal clear. Thanks!
Loving your tutorial. Crystal clear explanation
you are the best programmer combining teacher as i ever met!support!
Clear and concise. Thank you
Great video. It is so clear now and books never explain it such great way
I thought I had a firm grasp of what it means when they say Strings are immutable. After watching this video, Everything i thought i knew went straight outta the window. Thanks John. Way to make things understandable.
Highly appreciate your efforts.
John! You explained it very well. Completely understandable and with a really nice example.
Thanks for the effort you put in these videos. Thanks for sharing. I am learning a lot.
i wasn't aware of the benefits of immutability, even though i knew exactly what immutability means. thanks for the tid bits!
Excellent video, and clearly explained.
Man! This is a mind blowing info for me. Never thought about this before! Thanks.
Thank you, very simple and clear explanation.
U are gifted!! Thanks for sharing!!
Wow, this was great! Super clear explanation that makes me finally get it.
Maybe you have a video that will finally make me understand static methods! I'll have to check.
Beautifully explained. Very informative . Thank you !!
Hello John, your videos are incredibly helpful. I have learnt a lot from your videos. Do keep up the good work, you will go big on CZcams one day.
You're a great teacher and a great help. Thanks.
Learnt a whole lot about string immutability in 7minutes than I have in a year. Lol. Nice one
perfect explanations john thanks for the content
Holy shit the way you explain things is so simple and lucid, subbed!!!
AMAZING video, thanks!
You explain things extremely well.
Wow I didn't aware of this.thanks a lot John
the coolest thing about the immutable part(to me) is the multiple thread point. i have come to appreciate that after so many times i have replicated that fact for my objects while trying to avoid the synchronized keyword.
easy to understand thank you john
This video was really helpful. Thank you very much.
Great video, John!
Finally! I finally understand what immutable actually means. Thank you for this clear explanation.
Thank you sir! brilliant tutorial!
Excellent video, Thank you John.
Love how the CZcams recommendations algo work.glad i came across this video.. so well explained.. thank you
Love you man you got all my points clear about immutables....
Loved your video mate, you have a new subscriber 🍻
Perfect explanation. Thanks.
Great job John! Thank you
Trifecta is done! Very well explained, thank you!
Thanks much!
Thank you a lot! Great explanation 🔥
flawless explanation. subscribed
thanks a lot! very clear explanation!
Yesterday I had this question in my mind and today I saw this video. Thanks!
your videos really awesome. i found late but awesome contents 👍🏻
i have to say i learn something new every day via this channel .
Great explanation, love it, thanks!
Amazing explanation thank you so much!
That was a great explanation!
Wow, this is the best explanation I can find on youtube! thank you john
You are amazing at teaching stuff hats off!
Very well explainded, thanks
Amazing and Awesome Explanation of the concepts.