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

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

Пікірлер: 753

  • @etainad0085
    @etainad00852 жыл бұрын

    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 KZread, 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
    @MrYourSalvation2 жыл бұрын

    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

    8 ай бұрын

    👍

  • @CodingWithJohn

    @CodingWithJohn

    4 ай бұрын

    Really cool!

  • @mohitsanghai5455
    @mohitsanghai54552 жыл бұрын

    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

    3 ай бұрын

    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)?

  • @alexz4006
    @alexz40062 жыл бұрын

    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.

  • @NuanDaa
    @NuanDaa2 жыл бұрын

    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.

  • @Hufsa
    @Hufsa2 жыл бұрын

    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!

  • @swapode
    @swapode2 жыл бұрын

    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

  • @idkidkidk3333
    @idkidkidk33332 жыл бұрын

    Thank you! I will for sure be checking out more of your videos. You explained everything very clearly!

  • @matthewharrison3921
    @matthewharrison39212 жыл бұрын

    You have a talent for explaining things in a concise and easy to process manner. Thank you, sir!

  • @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.

  • @dmitriirodionov
    @dmitriirodionov2 жыл бұрын

    Outstanding and easy to understand explanation! Thank you for this video!

  • @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
    @hagnap2 жыл бұрын

    Such a simple topic explained so well, wish they explained it like this in my early CS courses

  • @1dagmawi
    @1dagmawi2 жыл бұрын

    You are easy to understand and always to the point. Glad I found you. Keep up the good work. Cheers.

  • @CuriousCyclist
    @CuriousCyclist2 жыл бұрын

    Thank you for taking the time to make this video. I have liked, subscribed and am commenting. Cheers!

  • @jiro.nemo.1
    @jiro.nemo.1 Жыл бұрын

    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.

  • @matthewmoore1307
    @matthewmoore13072 жыл бұрын

    I love how you’re drawing out with examples as you explain

  • @anluong8103
    @anluong81032 жыл бұрын

    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 :)

  • @arturillosmeriglia8029
    @arturillosmeriglia80292 жыл бұрын

    Great explanation! Easy to follow, clear and concise. You earned a new subscriber!

  • @drewbruggman
    @drewbruggman2 жыл бұрын

    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!

  • @huseyinkadioglu
    @huseyinkadioglu10 ай бұрын

    Simple and clear explanation John. I appreciate your work and effort. Amazing stuff!

  • @ChaseDetrick
    @ChaseDetrick9 ай бұрын

    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.

  • @arshandubey
    @arshandubey2 жыл бұрын

    To the point , Every bit of information provided in just 7 minutes. SUBSCRIBED !!!

  • @Coccoutube
    @Coccoutube Жыл бұрын

    Excellent lesson John! I'm learning a lot with you :)

  • @codingweb3904
    @codingweb39042 жыл бұрын

    Knew all this but still his way of explaining held me till the end.... His way of teaching is just amazing

  • @praveen123ist
    @praveen123ist Жыл бұрын

    I think I'll never look back again at why string is immutable, you made it super clear 👌🙏 awesome!!!!

  • @haoshecn
    @haoshecn2 жыл бұрын

    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.

  • @mirzaaqeel
    @mirzaaqeel Жыл бұрын

    Crystal clear with examples. Hats off to you.

  • @h-girlradio4699
    @h-girlradio46992 жыл бұрын

    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!!

  • @supun_sandaruwan
    @supun_sandaruwan2 жыл бұрын

    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 🤩💖

  • @alexandrprodan8794
    @alexandrprodan8794 Жыл бұрын

    Wow, it's great video! Informative, comprehensive and not boring. Thank you very much!

  • @TG-ri3mu
    @TG-ri3mu2 жыл бұрын

    Wow, wow, wow, FINALLY the explanation of immutable strings that l was looking for sooo long time! THANK YOU!

  • @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!

  • @misspotato813
    @misspotato8132 жыл бұрын

    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!

  • @Rational_Human1

    @Rational_Human1

    2 жыл бұрын

    @@jake9854 which dark age r u living in ?

  • @herberthsu1495
    @herberthsu14952 жыл бұрын

    simple, clear and straightforward explanation. thanks for sharing

  • @StobbeTime
    @StobbeTime Жыл бұрын

    Your an awesome teacher! I remember studying this topic in my book and could not understand it till now. Thanks.

  • @mesikamesika
    @mesikamesika2 жыл бұрын

    You simplify it in an amazing way well done :)

  • @tiagosilva9795
    @tiagosilva9795Ай бұрын

    What a great explanation! Thanks John!

  • @dylangovender
    @dylangovender2 жыл бұрын

    This was awesome. Thanks, John!

  • @prajwaltirpude8582
    @prajwaltirpude85822 жыл бұрын

    Great video. It is so clear now and books never explain it such great way

  • @perox1982
    @perox1982 Жыл бұрын

    Now Everything has sence. I know many teachers that needs THIS VIDEOS. You Rocks John.

  • @vasundharachintha5037
    @vasundharachintha503710 ай бұрын

    Your videos are amazing!!You really do make the concepts clear!!Thanks for such an amazing content.

  • @syedirfanahmad9626
    @syedirfanahmad96262 жыл бұрын

    Good, quick description 👍

  • @Ali-cf5lm
    @Ali-cf5lm2 жыл бұрын

    Thank you John.. That was the best explanation for understanding what immutable means

  • @boxerlobsters
    @boxerlobsters Жыл бұрын

    Kudos to you for explaining in such a short time. Cheers man

  • @rajeshchattopadhyaya2805
    @rajeshchattopadhyaya28052 жыл бұрын

    I am currently learning Java your videos give deeper understanding of Java thank you very much!

  • @eiyukabe
    @eiyukabe2 жыл бұрын

    Excellent video, and clearly explained.

  • @undercovercoder
    @undercovercoder Жыл бұрын

    Thanks man!! Totally understood this concept :)

  • @LearnYouAndMe
    @LearnYouAndMe Жыл бұрын

    Loving your tutorial. Crystal clear explanation

  • @thunderstruck2727
    @thunderstruck27272 жыл бұрын

    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.

  • @JessePoage
    @JessePoage2 жыл бұрын

    Again, thank you for explaining concepts better than most professors

  • @rastkostajic5525
    @rastkostajic55254 ай бұрын

    Man! This is a mind blowing info for me. Never thought about this before! Thanks.

  • @rhitamdutta1996
    @rhitamdutta19962 жыл бұрын

    Holy shit the way you explain things is so simple and lucid, subbed!!!

  • @hackmasters617
    @hackmasters61710 күн бұрын

    Loved the explanation John!

  • @fantasy9960
    @fantasy9960 Жыл бұрын

    Wow, this is the best explanation I can find on youtube! thank you john

  • @Atharva_kathiyawadi
    @Atharva_kathiyawadi2 жыл бұрын

    your videos really awesome. i found late but awesome contents 👍🏻

  • @241sanchita
    @241sanchita2 жыл бұрын

    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 KZread one day.

  • @msampson3d
    @msampson3d2 жыл бұрын

    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.

  • @alexmercer8984
    @alexmercer89842 жыл бұрын

    Loved your video mate, you have a new subscriber 🍻

  • @TheRealAnkitSharma
    @TheRealAnkitSharma Жыл бұрын

    John! You explained it very well. Completely understandable and with a really nice example.

  • @jayskieeee
    @jayskieeee2 жыл бұрын

    i wasn't aware of the benefits of immutability, even though i knew exactly what immutability means. thanks for the tid bits!

  • @CodingWithGhostInCode
    @CodingWithGhostInCode Жыл бұрын

    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.

  • 2 жыл бұрын

    Damn such a nice work John, instant Sub!

  • @faizalimuhammadzoda4731
    @faizalimuhammadzoda47312 жыл бұрын

    Thanks for the effort you put in these videos. Thanks for sharing. I am learning a lot.

  • @hannahdlh1
    @hannahdlh12 жыл бұрын

    The best explanation for this topic that I've seen so far. Thank you!!

  • @emekaukwuoma3359
    @emekaukwuoma33592 жыл бұрын

    Learnt a whole lot about string immutability in 7minutes than I have in a year. Lol. Nice one

  • @raihanulalamhridoy4714
    @raihanulalamhridoy47142 жыл бұрын

    This video was really helpful. Thank you very much.

  • @zishanamin3607
    @zishanamin36072 жыл бұрын

    Protect this man at all costs. Best explanations on Java concepts on the internet

  • @billwu7367
    @billwu7367 Жыл бұрын

    you are the best programmer combining teacher as i ever met!support!

  • @THE16THPHANTOM
    @THE16THPHANTOM2 жыл бұрын

    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.

  • @hrf4711
    @hrf47112 жыл бұрын

    Wow I didn't aware of this.thanks a lot John

  • @r_majka
    @r_majka2 жыл бұрын

    It's very interesting to know what's happening behind the scenes. Very nice explanation :)

  • @bonnydavis6730
    @bonnydavis6730 Жыл бұрын

    Beautifully explained. Very informative . Thank you !!

  • @Bersling
    @Bersling2 жыл бұрын

    just completing the trifecta: yay, somebody does cool java videos about interesting topics in 2021 - subscribed!

  • @learnjavaprogramming7132
    @learnjavaprogramming71322 жыл бұрын

    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 ☺️☺️

  • @alexeysytnikov6220
    @alexeysytnikov62202 ай бұрын

    Great video, John!

  • @pendago8484
    @pendago84842 жыл бұрын

    You're a great teacher and a great help. Thanks.

  • @amaanmohammad1190
    @amaanmohammad11902 жыл бұрын

    Simple Concise and to the point.

  • @rrroman870
    @rrroman8702 жыл бұрын

    Crystal clear. Thanks!

  • @zzzalo1335
    @zzzalo13352 жыл бұрын

    Really useful info, Thanks for the video :D

  • @officialismailshah
    @officialismailshah2 жыл бұрын

    Love you man you got all my points clear about immutables....

  • @chidambaramvalliappan9069
    @chidambaramvalliappan9069 Жыл бұрын

    Amazing and Awesome Explanation of the concepts.

  • @krishnachaitu17
    @krishnachaitu17 Жыл бұрын

    perfect explanations john thanks for the content

  • @suneetlimkar
    @suneetlimkar2 жыл бұрын

    Love how the KZread recommendations algo work.glad i came across this video.. so well explained.. thank you

  • @rajaaekant
    @rajaaekant2 жыл бұрын

    Clear and concise. Thank you

  • @cr.6848
    @cr.68482 жыл бұрын

    Thank you a lot! Great explanation 🔥

  • @VandanaArora
    @VandanaArora2 жыл бұрын

    That was a very informative video!🔥

  • @levekbarel
    @levekbarel2 жыл бұрын

    U are gifted!! Thanks for sharing!!

  • @pinkzombierabbit
    @pinkzombierabbit Жыл бұрын

    Thank you, very simple and clear explanation.

  • @tobito__
    @tobito__2 жыл бұрын

    Amazing content. I dont usually comment but your way of teaching is extraordinary! Keep them good contents coming.

  • @Michael17000
    @Michael170002 жыл бұрын

    Great explanation, love it, thanks!

  • @user-lx9nk7vf8c
    @user-lx9nk7vf8c2 жыл бұрын

    Yesterday I had this question in my mind and today I saw this video. Thanks!

  • @sourav5449
    @sourav54496 ай бұрын

    This is the best explanation of Immutability on youtube You are a gem 💎

  • @codyprevatt
    @codyprevatt2 ай бұрын

    You explain things extremely well.

  • @anujrathore2517
    @anujrathore25172 жыл бұрын

    This was amazing video And really well explained

  • @midyaazad7215
    @midyaazad7215 Жыл бұрын

    Waw, so valuable information in just a few minutes. Thanks

  • @JoelAguilarCerna
    @JoelAguilarCerna2 жыл бұрын

    I learnt something valuable today! Thanks a lot!

  • @harit9380
    @harit93802 жыл бұрын

    That was a great explanation!

  • @humairanaeempasha2362
    @humairanaeempasha23622 жыл бұрын

    You are amazing at teaching stuff hats off!

  • @shuchitam3868
    @shuchitam38682 жыл бұрын

    thanks for such an easy explanation! save so much time..