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

preview_player
Показать описание
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

📕 One of my favorite programming books, Clean Code by Robert Martin

🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial

🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)

📹Phone I use for recording:

🎙️Microphone I use (classy, I know):

Donate with PayPal (Thank you so much!)

☕Complete Java course:

Рекомендации по теме
Комментарии
Автор

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

etainad
Автор

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.

MrYourSalvation
Автор

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

mohitsanghai
Автор

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.

alexz
Автор

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.

NuanDaa
Автор

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.

swapode
Автор

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

matthewharrison
Автор

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!

Hufsa
Автор

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

Fadhelalmalki
Автор

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!

misspotato
Автор

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.

jiro.nemo.
Автор

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.

haoshecn
Автор

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.

rahulshetty
Автор

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

zishanamin
Автор

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

anluong
Автор

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

rhitamdutta
Автор

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

praveenist
Автор

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

sourav
Автор

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

abolfazlsaravani
Автор

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.

SuprousOxide