.equals() vs. == in Java - The Real Difference

preview_player
Показать описание
Watch now to learn the difference between ".equals()" vs. "==" in Java. Full tutorial on the difference between "==" (double equals) and the ".equals()" method in Java. Especially for Strings, when do you use ==, and when do you use .equals?

Simple equality checks in Java can be tricky, but the facts are simpler than you may think.

This is a very beginner friendly beginner's Java coding lesson tutorial, where we'll discuss the difference between == and .equals() when doing equals checks in Java.

Learn or improve your Java by watching it being coded live!

Hey, I'm John! I'm a Lead Java Software Engineer who has been in the 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 developers.

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:

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

so far I've watched several videos with the same topic but this video only explained well. Thanks man!

rykageee
Автор

Warning ⚠️ : If you are going to override .equals method, you have to override your hascode method. Otherwise there will be a mess with those objects whenever you hashing

praveens
Автор

Oh my god I'm genuinely so thankful I discovered this Channel, John explains stuff like no one else does, you just earned yourself a subscriber mate, thank you for all the efforts.❤

Daresigmally
Автор

John, this was an amazing tutorial!

I had a query though; on the objects, do we override the equals() or is it inferred?

dhavewala
Автор

I'm a newbie dev and I finished a bootcamp but your videos topics are all the topics I found confusing during class and always wondered about. TY so much.

magicalhats
Автор

Simple and easy to understand. Thank you!

Visionist
Автор

But in your strings are immutable video, you said when initializing a string variable java points it to a pre existing string memory address. With that, here hello is already exist when you initialize the second string. Therefore both variable should point to same address, that is using == returns true. ? I am confused plz answer me.

binilgeorge
Автор

I'm glad I've found this chanel. I've never been more educated by any other escaped convict in my entire life! Keep it up! :)

brtkgromek
Автор

Really fascinated at how good you are able to explain. Thanks for the great video!

streamity
Автор

Man they rejected me yesterday based on this question

radhemohannaamhai
Автор

is it too bad if we declare Strings as literal all the time? like String string1= "hello" instead a declaring an object with it? I mean is there a reason to declare an object String?.

brunomartel
Автор

That was incredibly well explained! Thanks a lot!

ovey
Автор

Small piece of feedback: title shoulda been ".equals() and == are not equal"

Thanks for the great video!

micahrufsvold
Автор

What’s the codes for .equals() in class Dog?

Amy-zbiq
Автор

Hey pls make video on comparable comparator interface in java

InfoBuzz
Автор

You are great. Congrats for channel and being Team Leader. Maybe you could/should make a video talking about what should a junior know. I think i know equals now, etc, im learning generics and i undertand but i saw a generic method in an interview(company was about HR, and i was asked 4 Design Patterns which i knew but wasnt good enough for company anyway) i had no idea what it was even if i knew generics it was kind of pro code/ultra weird and hard i think it was with lists btw..i also hate leetcode i prefer videos like yours to actually learn java. I would also learn more from you cause you good and you enjoyable i hope you do the same in your job irl. i think i also know most of collections hashmap/set etc

GenjaOrigins
Автор

I got back into Java recently after not using it for like 8 years and have been haphazardly using == for strings and it has been working, presumably because I have been always using strings as state variables in games, and so have been manually typing the strings in my conditions as literals (if (facing == "up")) etc.

I saw a comment on another video and I'm glad I found this early enough to refactor everything. haha.

dotanon
Автор

Feeling lucky to have found this channel, I've been self studying on and off for a couple years and I'm pretty sure I should have known these key concepts by now, sincere thanks for these clear and understandable teachings!

cjm
Автор

These are beautiful videos man 😁 Every sentence of yours resolves an issue I had and leads to the next question I had. So helpful ! Thanks 🙂

olivierbegassat
Автор

See this is interesting, because I thought with Javas memory management scheme that it uses and forces strings to be immutable would reference the same object in memory, even though the variables are two seperate ones.

Eg. I was under the impression that
String string1 = "James";
String string2 = "James";

these would both point to the same object in memory because these are the same value, which you would think would cause the == operator to go "Oh hey, this is true!".

MasonJF