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

Поділитися
Вставка
  • Опубліковано 23 січ 2025

КОМЕНТАРІ • 768

  • @etainad0085
    @etainad0085 3 роки тому +755

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

    • @CodingWithJohn
      @CodingWithJohn  3 роки тому +79

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

    • @cagan8
      @cagan8 3 роки тому +14

      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 3 роки тому +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 3 роки тому +220

    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 Рік тому

      👍

    • @CodingWithJohn
      @CodingWithJohn  10 місяців тому +4

      Really cool!

  • @mohitsanghai5455
    @mohitsanghai5455 2 роки тому +54

    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 9 місяців тому

      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 3 роки тому +50

    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
    @NuanDaa 2 роки тому +13

    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.

  • @AdrianWP_T
    @AdrianWP_T 3 місяці тому +3

    I watched your videos countless times while doing my online courses. (I got my degree in 2005 in mechanical engineering but made a serious career change this year) Now, years down the line I'm sitting as an intern as a junior software developer (at age 42), and just watched yet another video. Your explanations are crystal clear. Thanks for the effort in creating them - much appreciated.

  • @Hufsa
    @Hufsa 3 роки тому +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!

  • @prajwalapattanshetti1743
    @prajwalapattanshetti1743 19 днів тому

    I really really don't comment on any UA-cam videos as such. But man!!!! The way you explain, your examples, everything is just top notch! I love your videos and have learnt so much from you. Thank you so much!

  • @anluong8103
    @anluong8103 2 роки тому +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 :)

  • @matthewharrison3921
    @matthewharrison3921 2 роки тому +8

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

  • @jiro.nemo.1
    @jiro.nemo.1 2 роки тому +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.

  • @LeninMcDonalds
    @LeninMcDonalds 4 місяці тому

    John you have a gift for teaching. Only a Java master can explain the basics with such clarity to newbies.

  • @Veganzza
    @Veganzza 2 роки тому

    Man, you are so amazing. I've read many guides about immutable string but none of them is as clear as your explanation

  • @perox1982
    @perox1982 2 роки тому

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

  • @misspotato813
    @misspotato813 3 роки тому +9

    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!

  • @edwardthng2502
    @edwardthng2502 3 місяці тому

    Im literally studying for AP CS and i couldnt understand why a new string would give a false output, until I came across your video. Your a lifesaver.

  • @hanikhellafi4872
    @hanikhellafi4872 2 роки тому +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.

  • @rhitamdutta1996
    @rhitamdutta1996 3 роки тому +2

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

  • @idkidkidk3333
    @idkidkidk3333 3 роки тому +12

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

  • @simonjunior184
    @simonjunior184 3 роки тому +4

    Very helpful demonstration of the concept. I hardly found other videos confusing to understand. Keep up the great job bro!

    • @herrbonk3635
      @herrbonk3635 3 роки тому

      But you find this one confusing to understand??

    • @simonjunior184
      @simonjunior184 3 роки тому

      @@herrbonk3635 I found this one very clear and straight forward to understand. I went through the video along coding on my IDE.

    • @herrbonk3635
      @herrbonk3635 3 роки тому

      @@simonjunior184 Like most other videos then? (You said you hardly found them confusing, which means that you don't find them confusing.)

  • @CodingWithGhostInCode
    @CodingWithGhostInCode 2 роки тому

    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.

  • @praveen123ist
    @praveen123ist 2 роки тому +1

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

  • @matthewmoore1307
    @matthewmoore1307 3 роки тому +1

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

  • @zishanamin3607
    @zishanamin3607 3 роки тому +8

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

  • @rahulshetty9335
    @rahulshetty9335 3 роки тому +1

    Never even used Java a lot but I have been asked this question in interview. But this is the first time I understood what I was telling the interviewer.

  • @Fadhelalmalki
    @Fadhelalmalki 6 місяців тому

    I can swear this man is really a man and know how I think giving the needed answer

  • @billwu7367
    @billwu7367 2 роки тому

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

  • @hagnap
    @hagnap 3 роки тому +6

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

  • @drewbruggman
    @drewbruggman 3 роки тому

    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!

  • @jayskieeee
    @jayskieeee 3 роки тому +1

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

  • @sourav5449
    @sourav5449 Рік тому

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

  • @swastikmohapatra4665
    @swastikmohapatra4665 3 роки тому

    i have to say i learn something new every day via this channel .

  • @emekaukwuoma3359
    @emekaukwuoma3359 3 роки тому +1

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

  • @fantasy9960
    @fantasy9960 Рік тому

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

  • @dmitriirodionov
    @dmitriirodionov 3 роки тому +10

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

  • @ChaseDetrick
    @ChaseDetrick Рік тому

    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.

  • @JessePoage
    @JessePoage 2 роки тому +1

    Again, thank you for explaining concepts better than most professors

  • @ragore7947
    @ragore7947 2 роки тому +1

    John, you are [ immutably ] awesome!

  • @learnjavaprogramming7132
    @learnjavaprogramming7132 3 роки тому

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

  • @codingweb3904
    @codingweb3904 3 роки тому

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

  • @arshandubey
    @arshandubey 3 роки тому

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

  • @SuprousOxide
    @SuprousOxide 2 роки тому +10

    Strings are also very commonly used as keys in Map collections. If you insert a value in a map with a key, and then change the key object , you will no longer be able to look up that value with the original key. And with some map types,may have trouble looking up other keys because you have a key stored in the wrong place.

    • @wamikgildiev6632
      @wamikgildiev6632 Рік тому

      When you add a value to a map you specify a key (a String in our case). A key is not an object, it's just a pointer to String object in memory. That String object is used to compute a hash code, and to find a bucket index later. So the question is Does Hashmap store a pointer to that String object and why if it already has computed the hash code? I could use Integer or any other class as HashMap key, but they are final.

    • @SuprousOxide
      @SuprousOxide Рік тому

      @@wamikgildiev6632 It needs to keep a the actual key around, not just the hash code, because of hash collisions. If two keys have the same hash-code, you need to compare the actual key values to tell which (if either) is pointing at the value you want.
      If you actually make a copy of the key when you insert it, that would probably work, but I think most implementations assume your key is immutable.

  • @Bersling
    @Bersling 3 роки тому

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

  • @CuriousCyclist
    @CuriousCyclist 3 роки тому

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

  • @swapode
    @swapode 3 роки тому +251

    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 3 роки тому +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 3 роки тому +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 3 роки тому +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 3 роки тому +21

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

    • @garybuttherissilent5896
      @garybuttherissilent5896 3 роки тому +5

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

  • @ronniesuvillian4893
    @ronniesuvillian4893 2 роки тому

    WOWW!! I am a C++ Developer and i dont even know java, but this discussion was really interesting. Thanks for the information John!!

  • @huseyinkadioglu
    @huseyinkadioglu Рік тому

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

  • @rajeshchattopadhyaya2805
    @rajeshchattopadhyaya2805 3 роки тому

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

  • @haoshecn
    @haoshecn 3 роки тому +31

    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.

  • @THE16THPHANTOM
    @THE16THPHANTOM 3 роки тому

    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.

  • @boxerlobsters
    @boxerlobsters 2 роки тому

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

  • @supun_sandaruwan
    @supun_sandaruwan 3 роки тому +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 🤩💖

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

  • @thunderstruck2727
    @thunderstruck2727 3 роки тому +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  3 роки тому +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 3 роки тому

      @@CodingWithJohn thanks for the reply! I’m a Comp Sci graduate who just finished my first year working a software job.

  • @odravison
    @odravison 2 роки тому

    Hey John, just to tell you that your video saved my carrer! Thanks a lot! Keep going.

    • @CodingWithJohn
      @CodingWithJohn  2 роки тому +1

      Wow, I'd love to hear how!

    • @odravison
      @odravison 2 роки тому

      @@CodingWithJohn hahah! OK. I shouldn't be that extremy in my words haha!
      I just watched this video just before I taken an interview within an interviwer who asked for me this concept in Java xD

    • @odravison
      @odravison 2 роки тому

      @@CodingWithJohn haha, ok, I could be extreme when I said that haha. But this video helped me to answer my interview question correctly thhhxxx

  • @rastkostajic5525
    @rastkostajic5525 10 місяців тому

    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

  • @abolfazlsaravani2214
    @abolfazlsaravani2214 3 роки тому +3

    God I love Java, after 5 years of Java programming, I still love how it works.

  • @drewbhardwaj
    @drewbhardwaj 2 роки тому

    probably the best teacher for coding.🙏🙏

  • @mohitjaiswal3526
    @mohitjaiswal3526 2 роки тому

    Now , I really understand . Why Java Strings are Immutable? Thank you John Sir

  • @TG-ri3mu
    @TG-ri3mu 3 роки тому

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

  • @DragonKlavier
    @DragonKlavier 2 роки тому

    Omg. I just discovered this magnifique Chanel to really learn Java.... thanks!!!!

  • @Gabriel-bu1qq
    @Gabriel-bu1qq 2 роки тому

    Hey John, just amazing channel, i subscribed already. I finally could understand this. just a tip in my humble opinion, if you could speak not so fast i had to slowdown the video a little bit to follow your thoughts, but its just a tip the rest its perfect. Congratulations!!!!!

  • @StobbeTime
    @StobbeTime 2 роки тому

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

  • @mirzaaqeel
    @mirzaaqeel 2 роки тому

    Crystal clear with examples. Hats off to you.

  • @BlackHazama
    @BlackHazama 2 роки тому

    Never fully understood why strings were immutable or the reason behind them. Thank you!

  • @alexandrprodan8794
    @alexandrprodan8794 2 роки тому

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

  • @tiagosilva9795
    @tiagosilva9795 7 місяців тому

    What a great explanation! Thanks John!

  • @vasundharachintha5037
    @vasundharachintha5037 Рік тому

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

  • @Coccoutube
    @Coccoutube 2 роки тому

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

  • @ЛёхаБодунов-б7ж
    @ЛёхаБодунов-б7ж 3 роки тому

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

  • @bonnydavis6730
    @bonnydavis6730 Рік тому

    Beautifully explained. Very informative . Thank you !!

  • @harknerd1775
    @harknerd1775 9 днів тому

    Thanks for describing why it was designed this way! I still don't agree with the decision or like it, but now I understand it

  • @dylangovender
    @dylangovender 2 роки тому

    This was awesome. Thanks, John!

  • @Jakypbekovich
    @Jakypbekovich 2 роки тому

    Trifecta is done! Very well explained, 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!

  • @maresolaris
    @maresolaris 2 роки тому

    Finally! I finally understand what immutable actually means. Thank you for this clear explanation.

  • @hackmasters617
    @hackmasters617 6 місяців тому

    Loved the explanation John!

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

  • @tobito__
    @tobito__ 2 роки тому

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

  • @codyprevatt
    @codyprevatt 8 місяців тому

    You explain things extremely well.

  • @pgm2004
    @pgm2004 3 роки тому

    this man really out here saving my grades. god BLESS

  • @amaanmohammad1190
    @amaanmohammad1190 2 роки тому

    Simple Concise and to the point.

  • @nothappyz
    @nothappyz 2 роки тому

    Worked with java for years, and never knew the pool thing. Amazing

  • @1dagmawi
    @1dagmawi 3 роки тому

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

  • @arturillosmeriglia8029
    @arturillosmeriglia8029 2 роки тому

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

  • @alexmercer8984
    @alexmercer8984 2 роки тому

    Loved your video mate, you have a new subscriber 🍻

  • @LearnYouAndMe
    @LearnYouAndMe 2 роки тому

    Loving your tutorial. Crystal clear explanation

  •  3 роки тому

    Damn such a nice work John, instant Sub!

  • @alexeysytnikov6220
    @alexeysytnikov6220 8 місяців тому

    Great video, John!

  • @hannahdlh1
    @hannahdlh1 3 роки тому +6

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

  • @suneetlimkar
    @suneetlimkar 3 роки тому

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

  • @eiyukabe
    @eiyukabe 3 роки тому +1

    Excellent video, and clearly explained.

  • @PradumTiwari28
    @PradumTiwari28 2 роки тому

    Really your teaching of every concept is legendary keep Doing good work

  • @sb-zn4um
    @sb-zn4um 3 роки тому

    finally an understanding of why .equals was always preferential over ==

  • @syedirfanahmad9626
    @syedirfanahmad9626 2 роки тому

    Good, quick description 👍

  • @wedifilipoprogrammers1073
    @wedifilipoprogrammers1073 3 роки тому

    I always procrastinated to search about strings being immutable and here its recommended by UA-cam

  • @r_majka
    @r_majka 2 роки тому

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

  • @officialismailshah
    @officialismailshah 3 роки тому

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

  • @humairanaeempasha2362
    @humairanaeempasha2362 2 роки тому

    You are amazing at teaching stuff hats off!