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

  Рет қаралды 605,514

Coding with John

Coding with John

Күн бұрын

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

Пікірлер: 749
@etainad0085
@etainad0085 2 жыл бұрын
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 KZbin, a 100%!
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Thanks a ton! I'll keep working on it, glad it's helping you!
@cagan8
@cagan8 2 жыл бұрын
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
@alexwatts1375
@alexwatts1375 2 жыл бұрын
The best creators bubble to the top!
@snowwhite2146
@snowwhite2146 2 жыл бұрын
@@CodingWithJohn Same here. I have a Java exam in June and your videos are a good revision for me. Many thanks.
@csgrinds
@csgrinds 2 жыл бұрын
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.
@MrYourSalvation
@MrYourSalvation 2 жыл бұрын
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.
@finmonster5827
@finmonster5827 2 жыл бұрын
ґалічєнин
@annaburdanova8307
@annaburdanova8307 2 жыл бұрын
Yura, thank you for sharing this interesting fact, the video itself and the comments are a real treasure for a Java learner!
@fabio.1
@fabio.1 7 ай бұрын
👍
@CodingWithJohn
@CodingWithJohn 3 ай бұрын
Really cool!
@mohitsanghai5455
@mohitsanghai5455 2 жыл бұрын
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
@bobfarker4001
@bobfarker4001 Жыл бұрын
You basically summarized the summary.
@Atarian6502
@Atarian6502 2 ай бұрын
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)?
@Hufsa
@Hufsa 2 жыл бұрын
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!
@alexz4006
@alexz4006 2 жыл бұрын
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.
@idkidkidk3333
@idkidkidk3333 2 жыл бұрын
Thank you! I will for sure be checking out more of your videos. You explained everything very clearly!
@NuanDaa
@NuanDaa 2 жыл бұрын
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.
@matthewharrison3921
@matthewharrison3921 Жыл бұрын
You have a talent for explaining things in a concise and easy to process manner. Thank you, sir!
@dmitriirodionov
@dmitriirodionov 2 жыл бұрын
Outstanding and easy to understand explanation! Thank you for this video!
@hanikhellafi4872
@hanikhellafi4872 Жыл бұрын
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.
@swapode
@swapode 2 жыл бұрын
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.
@XD-nj7bc
@XD-nj7bc 2 жыл бұрын
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.
@watertrainer3992
@watertrainer3992 2 жыл бұрын
@@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
@XD-nj7bc
@XD-nj7bc 2 жыл бұрын
@@watertrainer3992 of course it is not cheaper than not mutable strings but you can then change the size without the copying he mentionend.
@swapode
@swapode 2 жыл бұрын
@@XD-nj7bc I put the word "sensible" in there for a reason 🙂
@garybuttherissilent5896
@garybuttherissilent5896 2 жыл бұрын
@@XD-nj7bc yeah he put the word "sensible" in there for a reason bruh
@1dagmawi
@1dagmawi 2 жыл бұрын
You are easy to understand and always to the point. Glad I found you. Keep up the good work. Cheers.
@Veganzza
@Veganzza Жыл бұрын
Man, you are so amazing. I've read many guides about immutable string but none of them is as clear as your explanation
@hagnap
@hagnap 2 жыл бұрын
Such a simple topic explained so well, wish they explained it like this in my early CS courses
@matthewmoore1307
@matthewmoore1307 2 жыл бұрын
I love how you’re drawing out with examples as you explain
@CuriousCyclist
@CuriousCyclist 2 жыл бұрын
Thank you for taking the time to make this video. I have liked, subscribed and am commenting. Cheers!
@mirzaaqeel
@mirzaaqeel Жыл бұрын
Crystal clear with examples. Hats off to you.
@arshandubey
@arshandubey 2 жыл бұрын
To the point , Every bit of information provided in just 7 minutes. SUBSCRIBED !!!
@anluong8103
@anluong8103 Жыл бұрын
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 :)
@huseyinkadioglu
@huseyinkadioglu 9 ай бұрын
Simple and clear explanation John. I appreciate your work and effort. Amazing stuff!
@herberthsu1495
@herberthsu1495 2 жыл бұрын
simple, clear and straightforward explanation. thanks for sharing
@TG-ri3mu
@TG-ri3mu 2 жыл бұрын
Wow, wow, wow, FINALLY the explanation of immutable strings that l was looking for sooo long time! THANK YOU!
@perox1982
@perox1982 Жыл бұрын
Now Everything has sence. I know many teachers that needs THIS VIDEOS. You Rocks John.
@arturillosmeriglia8029
@arturillosmeriglia8029 2 жыл бұрын
Great explanation! Easy to follow, clear and concise. You earned a new subscriber!
@Coccoutube
@Coccoutube Жыл бұрын
Excellent lesson John! I'm learning a lot with you :)
@alexandrprodan8794
@alexandrprodan8794 Жыл бұрын
Wow, it's great video! Informative, comprehensive and not boring. Thank you very much!
@undercovercoder
@undercovercoder Жыл бұрын
Thanks man!! Totally understood this concept :)
@tiagosilva9795
@tiagosilva9795 13 күн бұрын
What a great explanation! Thanks John!
@syedirfanahmad9626
@syedirfanahmad9626 2 жыл бұрын
Good, quick description 👍
@Ali-cf5lm
@Ali-cf5lm 2 жыл бұрын
Thank you John.. That was the best explanation for understanding what immutable means
@h-girlradio4699
@h-girlradio4699 2 жыл бұрын
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!!
@amaanmohammad1190
@amaanmohammad1190 2 жыл бұрын
Simple Concise and to the point.
@pinkzombierabbit
@pinkzombierabbit Жыл бұрын
Thank you, very simple and clear explanation.
@vasundharachintha5037
@vasundharachintha5037 9 ай бұрын
Your videos are amazing!!You really do make the concepts clear!!Thanks for such an amazing content.
@boxerlobsters
@boxerlobsters Жыл бұрын
Kudos to you for explaining in such a short time. Cheers man
@rastkostajic5525
@rastkostajic5525 3 ай бұрын
Man! This is a mind blowing info for me. Never thought about this before! Thanks.
@prajwaltirpude8582
@prajwaltirpude8582 2 жыл бұрын
Great video. It is so clear now and books never explain it such great way
@dylangovender
@dylangovender 2 жыл бұрын
This was awesome. Thanks, John!
@StobbeTime
@StobbeTime Жыл бұрын
Your an awesome teacher! I remember studying this topic in my book and could not understand it till now. Thanks.
@raihanulalamhridoy4714
@raihanulalamhridoy4714 2 жыл бұрын
This video was really helpful. Thank you very much.
@bonnydavis6730
@bonnydavis6730 11 ай бұрын
Beautifully explained. Very informative . Thank you !!
@mesikamesika
@mesikamesika 2 жыл бұрын
You simplify it in an amazing way well done :)
@rajaaekant
@rajaaekant 2 жыл бұрын
Clear and concise. Thank you
@LearnYouAndMe
@LearnYouAndMe Жыл бұрын
Loving your tutorial. Crystal clear explanation
@drewbruggman
@drewbruggman 2 жыл бұрын
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!
@chidambaramvalliappan9069
@chidambaramvalliappan9069 Жыл бұрын
Amazing and Awesome Explanation of the concepts.
@praveen123ist
@praveen123ist Жыл бұрын
I think I'll never look back again at why string is immutable, you made it super clear 👌🙏 awesome!!!!
@rrroman870
@rrroman870 2 жыл бұрын
Crystal clear. Thanks!
@codingweb3904
@codingweb3904 2 жыл бұрын
Knew all this but still his way of explaining held me till the end.... His way of teaching is just amazing
@JustinBieberFanSA
@JustinBieberFanSA Жыл бұрын
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!
@eiyukabe
@eiyukabe 2 жыл бұрын
Excellent video, and clearly explained.
@krishnachaitu17
@krishnachaitu17 Жыл бұрын
perfect explanations john thanks for the content
@abhay12323
@abhay12323 2 жыл бұрын
Highly appreciate your efforts.
@supun_sandaruwan
@supun_sandaruwan 2 жыл бұрын
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 🤩💖
@codyprevatt
@codyprevatt Ай бұрын
You explain things extremely well.
@faizalimuhammadzoda4731
@faizalimuhammadzoda4731 2 жыл бұрын
Thanks for the effort you put in these videos. Thanks for sharing. I am learning a lot.
@billwu7367
@billwu7367 Жыл бұрын
you are the best programmer combining teacher as i ever met!support!
@levekbarel
@levekbarel 2 жыл бұрын
U are gifted!! Thanks for sharing!!
@shankar7435
@shankar7435 Жыл бұрын
Perfect explanation. Thanks.
@rajeshchattopadhyaya2805
@rajeshchattopadhyaya2805 2 жыл бұрын
I am currently learning Java your videos give deeper understanding of Java thank you very much!
@xuansong4795
@xuansong4795 2 жыл бұрын
Excellent video, Thank you John.
@rhitamdutta1996
@rhitamdutta1996 2 жыл бұрын
Holy shit the way you explain things is so simple and lucid, subbed!!!
@haoshecn
@haoshecn 2 жыл бұрын
Hi, John. Love your tutorials, but I think there is a little thing you didn’t explain in details here. When you new a string “John”, the object is indeed created in the heap, but “John” is still crated inside string pool. There is a reference inside the object pointing towards “John”. So, you actually created two objects when you use ‘new’ keyword to create a string.
@fightinggold7674
@fightinggold7674 Жыл бұрын
flawless explanation. subscribed
@sebon11
@sebon11 Жыл бұрын
AMAZING video, thanks!
@AmongUs-qt3uo
@AmongUs-qt3uo Жыл бұрын
Thank you sir! brilliant tutorial!
@TheRealAnkitSharma
@TheRealAnkitSharma Жыл бұрын
John! You explained it very well. Completely understandable and with a really nice example.
@pendago8484
@pendago8484 2 жыл бұрын
You're a great teacher and a great help. Thanks.
@fantasy9960
@fantasy9960 Жыл бұрын
Wow, this is the best explanation I can find on youtube! thank you john
@hrf4711
@hrf4711 2 жыл бұрын
Wow I didn't aware of this.thanks a lot John
@Jakypbekovich
@Jakypbekovich Жыл бұрын
Trifecta is done! Very well explained, thank you!
@CodingWithJohn
@CodingWithJohn Жыл бұрын
Thanks much!
@Atharva_kathiyawadi
@Atharva_kathiyawadi 2 жыл бұрын
your videos really awesome. i found late but awesome contents 👍🏻
@officialismailshah
@officialismailshah 2 жыл бұрын
Love you man you got all my points clear about immutables....
@midyaazad7215
@midyaazad7215 Жыл бұрын
Waw, so valuable information in just a few minutes. Thanks
@thunderstruck2727
@thunderstruck2727 2 жыл бұрын
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!
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
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!
@thunderstruck2727
@thunderstruck2727 2 жыл бұрын
@@CodingWithJohn thanks for the reply! I’m a Comp Sci graduate who just finished my first year working a software job.
@ChaseDetrick
@ChaseDetrick 8 ай бұрын
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.
@kingfatpig3512
@kingfatpig3512 2 жыл бұрын
easy to understand thank you john
@AlshehriAhmed
@AlshehriAhmed Жыл бұрын
Excellent! Thank you for the explanation, outstanding!
@JessePoage
@JessePoage 2 жыл бұрын
Again, thank you for explaining concepts better than most professors
@LTZMSYAL
@LTZMSYAL Жыл бұрын
Great job John! Thank you
@humairanaeempasha2362
@humairanaeempasha2362 2 жыл бұрын
You are amazing at teaching stuff hats off!
@MohitKhare
@MohitKhare Жыл бұрын
fast and effective my friend. Thanks
@alexeysytnikov6220
@alexeysytnikov6220 Ай бұрын
Great video, John!
@marcosfraguela
@marcosfraguela 2 жыл бұрын
Very well explainded, thanks
@suneetlimkar
@suneetlimkar 2 жыл бұрын
Love how the KZbin recommendations algo work.glad i came across this video.. so well explained.. thank you
@sourav5449
@sourav5449 5 ай бұрын
This is the best explanation of Immutability on youtube You are a gem 💎
@OREH88
@OREH88 2 жыл бұрын
thanks a lot! very clear explanation!
@ns8928
@ns8928 2 жыл бұрын
Really great explanation! Thank you.
@harit9380
@harit9380 2 жыл бұрын
That was a great explanation!
@manojarya7676
@manojarya7676 2 жыл бұрын
Awesome Explanation man☺️☺️☺️👍
@jamesrussell-ui6gd
@jamesrussell-ui6gd Жыл бұрын
Thank you - really great explanation
@d0rzA
@d0rzA 2 жыл бұрын
Amazing explanation thank you so much!
@msampson3d
@msampson3d 2 жыл бұрын
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.
@shuchitam3868
@shuchitam3868 2 жыл бұрын
thanks for such an easy explanation! save so much time..
@pbgkhy
@pbgkhy 11 ай бұрын
Awesome, very clear examples. U the best teacher❤
@Michael17000
@Michael17000 2 жыл бұрын
Great explanation, love it, thanks!
@241sanchita
@241sanchita 2 жыл бұрын
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 KZbin one day.
@JoelAguilarCerna
@JoelAguilarCerna 2 жыл бұрын
I learnt something valuable today! Thanks a lot!
@alexmercer8984
@alexmercer8984 2 жыл бұрын
Loved your video mate, you have a new subscriber 🍻
@yapjinhao3598
@yapjinhao3598 2 жыл бұрын
Great explanation ! Thank you !
Static vs Non-Static Variables and Methods In Java - Full Simple Tutorial
11:29
.equals() vs. == in Java - The Real Difference
8:48
Coding with John
Рет қаралды 180 М.
We Got Expelled From Scholl After This...
00:10
Jojo Sim
Рет қаралды 75 МЛН
ИРИНА КАЙРАТОВНА - АЙДАХАР (БЕКА) [MV]
02:51
ГОСТ ENTERTAINMENT
Рет қаралды 7 МЛН
Day 7: Django Blog App part 1 || Django framework workshop
1:30:53
Dynamic Technologies
Рет қаралды 4
String Memory Allocation in Java (Stack Memory Vs Heap Memory Vs String Constant Pool)
9:55
Code-Travel-Explore-With-Harshal
Рет қаралды 16 М.
Exception Handling in Java Tutorial
13:20
Coding with John
Рет қаралды 365 М.
Array vs. ArrayList in Java Tutorial - What's The Difference?
17:36
Coding with John
Рет қаралды 499 М.
Java is ALWAYS Pass By Value. Here's Why
5:22
Coding with John
Рет қаралды 117 М.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1 МЛН
#35 Mutable vs Immutable String in Java
6:23
Telusko
Рет қаралды 100 М.
Optionals In Java - Simple Tutorial
15:53
Coding with John
Рет қаралды 199 М.