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

Поделиться
HTML-код
  • Опубликовано: 9 июл 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

Комментарии • 749

  • @etainad0085
    @etainad0085 2 года назад +725

    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 RUclips, a 100%!

    • @CodingWithJohn
      @CodingWithJohn  2 года назад +76

      Thanks a ton! I'll keep working on it, glad it's helping you!

    • @cagan8
      @cagan8 2 года назад +13

      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 года назад +3

      The best creators bubble to the top!

    • @snowwhite2146
      @snowwhite2146 2 года назад +3

      @@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 года назад +201

    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 года назад +7

      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 месяца назад +3

      Really cool!

  • @mohitsanghai5455
    @mohitsanghai5455 2 года назад +47

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

  • @alexz4006
    @alexz4006 2 года назад +49

    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.

  • @Hufsa
    @Hufsa 2 года назад +5

    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!

  • @NuanDaa
    @NuanDaa 2 года назад +11

    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.

  • @idkidkidk3333
    @idkidkidk3333 2 года назад +12

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

  • @hanikhellafi4872
    @hanikhellafi4872 Год назад +1

    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
    @dmitriirodionov 2 года назад +10

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

  • @matthewharrison3921
    @matthewharrison3921 Год назад +8

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

  • @1dagmawi
    @1dagmawi 2 года назад

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

  • @swapode
    @swapode 2 года назад +250

    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 года назад +4

      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 года назад +12

      @@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 года назад +5

      @@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 года назад +20

      @@XD-nj7bc I put the word "sensible" in there for a reason 🙂

    • @garybuttherissilent5896
      @garybuttherissilent5896 2 года назад +5

      @@XD-nj7bc yeah he put the word "sensible" in there for a reason bruh

  • @hagnap
    @hagnap 2 года назад +6

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

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

  • @CuriousCyclist
    @CuriousCyclist 2 года назад

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

  • @matthewmoore1307
    @matthewmoore1307 2 года назад +1

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

  • @anluong8103
    @anluong8103 Год назад +1

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

  • @Coccoutube
    @Coccoutube Год назад

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

  • @huseyinkadioglu
    @huseyinkadioglu 9 месяцев назад

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

  • @arturillosmeriglia8029
    @arturillosmeriglia8029 2 года назад

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

  • @TG-ri3mu
    @TG-ri3mu 2 года назад

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

  • @arshandubey
    @arshandubey 2 года назад

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

  • @mirzaaqeel
    @mirzaaqeel Год назад

    Crystal clear with examples. Hats off to you.

  • @vasundharachintha5037
    @vasundharachintha5037 9 месяцев назад

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

  • @LearnYouAndMe
    @LearnYouAndMe Год назад

    Loving your tutorial. Crystal clear explanation

  • @alexandrprodan8794
    @alexandrprodan8794 Год назад

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

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

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

  • @herberthsu1495
    @herberthsu1495 2 года назад

    simple, clear and straightforward explanation. thanks for sharing

  • @prajwaltirpude8582
    @prajwaltirpude8582 2 года назад

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

  • @undercovercoder
    @undercovercoder Год назад

    Thanks man!! Totally understood this concept :)

  • @mesikamesika
    @mesikamesika 2 года назад

    You simplify it in an amazing way well done :)

  • @boxerlobsters
    @boxerlobsters Год назад

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

  • @perox1982
    @perox1982 Год назад

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

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

  • @Ali-cf5lm
    @Ali-cf5lm 2 года назад

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

  • @rastkostajic5525
    @rastkostajic5525 4 месяца назад

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

  • @supun_sandaruwan
    @supun_sandaruwan 2 года назад +1

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

  • @faizalimuhammadzoda4731
    @faizalimuhammadzoda4731 2 года назад

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

  • @raihanulalamhridoy4714
    @raihanulalamhridoy4714 2 года назад

    This video was really helpful. Thank you very much.

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

  • @tiagosilva9795
    @tiagosilva9795 22 дня назад

    What a great explanation! Thanks John!

  • @codingweb3904
    @codingweb3904 2 года назад

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

  • @fantasy9960
    @fantasy9960 Год назад

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

  • @Atharva_kathiyawadi
    @Atharva_kathiyawadi 2 года назад

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

  • @rrroman870
    @rrroman870 2 года назад

    Crystal clear. Thanks!

  • @syedirfanahmad9626
    @syedirfanahmad9626 2 года назад

    Good, quick description 👍

  • @ChaseDetrick
    @ChaseDetrick 9 месяцев назад

    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.

  •  2 года назад

    Damn such a nice work John, instant Sub!

  • @eiyukabe
    @eiyukabe 2 года назад +1

    Excellent video, and clearly explained.

  • @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 RUclips one day.

  • @praveen123ist
    @praveen123ist Год назад +1

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

  • @rajeshchattopadhyaya2805
    @rajeshchattopadhyaya2805 2 года назад

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

  • @thunderstruck2727
    @thunderstruck2727 2 года назад +1

    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 года назад +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.

  • @bonnydavis6730
    @bonnydavis6730 Год назад

    Beautifully explained. Very informative . Thank you !!

  • @TheRealAnkitSharma
    @TheRealAnkitSharma Год назад

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

  • @rajaaekant
    @rajaaekant 2 года назад

    Clear and concise. Thank you

  • @krishnachaitu17
    @krishnachaitu17 Год назад

    perfect explanations john thanks for the content

  • @alexmercer8984
    @alexmercer8984 2 года назад

    Loved your video mate, you have a new subscriber 🍻

  • @pinkzombierabbit
    @pinkzombierabbit Год назад

    Thank you, very simple and clear explanation.

  • @rhitamdutta1996
    @rhitamdutta1996 2 года назад +2

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

  • @cr.6848
    @cr.6848 2 года назад

    Thank you a lot! Great explanation 🔥

  • @jiro.nemo.1
    @jiro.nemo.1 Год назад +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.

  • @pendago8484
    @pendago8484 2 года назад

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

  • @hrf4711
    @hrf4711 2 года назад

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

  • @officialismailshah
    @officialismailshah 2 года назад

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

  • @amaanmohammad1190
    @amaanmohammad1190 2 года назад

    Simple Concise and to the point.

  • @Michael17000
    @Michael17000 2 года назад

    Great explanation, love it, thanks!

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

  • @levekbarel
    @levekbarel 2 года назад

    U are gifted!! Thanks for sharing!!

  • @AmongUs-qt3uo
    @AmongUs-qt3uo Год назад

    Thank you sir! brilliant tutorial!

  • @kingfatpig3512
    @kingfatpig3512 2 года назад

    easy to understand thank you john

  • @xuansong4795
    @xuansong4795 2 года назад

    Excellent video, Thank you John.

  • @Bersling
    @Bersling 2 года назад

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

  • @LTZMSYAL
    @LTZMSYAL Год назад

    Great job John! Thank you

  • @alexeysytnikov6220
    @alexeysytnikov6220 Месяц назад

    Great video, John!

  • @billwu7367
    @billwu7367 Год назад

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

  • @evanoshea9506
    @evanoshea9506 Год назад

    insanely helpful, thanks!

  • @Urbanstrangler
    @Urbanstrangler 2 года назад

    Fascinating stuff, thanks!

  • @OREH88
    @OREH88 2 года назад

    thanks a lot! very clear explanation!

  • @sebon11
    @sebon11 Год назад

    AMAZING video, thanks!

  • @midyaazad7215
    @midyaazad7215 Год назад

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

  • @JessePoage
    @JessePoage 2 года назад +1

    Again, thank you for explaining concepts better than most professors

  • @abhay12323
    @abhay12323 2 года назад

    Highly appreciate your efforts.

  • @d0rzA
    @d0rzA 2 года назад

    Amazing explanation thank you so much!

  • @JoelAguilarCerna
    @JoelAguilarCerna 2 года назад

    I learnt something valuable today! Thanks a lot!

  • @humairanaeempasha2362
    @humairanaeempasha2362 2 года назад

    You are amazing at teaching stuff hats off!

  • @harit9380
    @harit9380 2 года назад

    That was a great explanation!

  • @VandanaArora
    @VandanaArora 2 года назад

    That was a very informative video!🔥

  • @shuchitam3868
    @shuchitam3868 2 года назад

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

  • @suneetlimkar
    @suneetlimkar 2 года назад

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

  • @zzzalo1335
    @zzzalo1335 2 года назад

    Really useful info, Thanks for the video :D

  • @ns8928
    @ns8928 2 года назад

    Really great explanation! Thank you.

  • @yapjinhao3598
    @yapjinhao3598 2 года назад

    Great explanation ! Thank you !

  • @tarikworku7908
    @tarikworku7908 2 года назад

    Great Idea with a great man! Thank u!

  • @chidambaramvalliappan9069
    @chidambaramvalliappan9069 Год назад

    Amazing and Awesome Explanation of the concepts.

  • @sgtdutra
    @sgtdutra 2 года назад

    Thank you!! It was a great explanation!

  • @AlshehriAhmed
    @AlshehriAhmed Год назад

    Excellent! Thank you for the explanation, outstanding!

  • @jayskieeee
    @jayskieeee 2 года назад +1

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