Java String Comparison Tutorial (Equals vs == in Java)

preview_player
Показать описание

You can compare strings in Java with either == or equals(). This video goes over when to use each. Most of the time you'll want to use equals to compare strings in Java because it compares the contents, not the object itself.

I hope you enjoyed this Java string comparison tutorial!

Disclosure: Springboard link provided is linked to my affiliate accounts & supports the channel.

Disclosure: The Springboard link provided is linked to my affiliate account & supports the channel.

~

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

The == operator checks if both variables points to the same memory location. So when you compared String a = "lemur" to String b = "lemur" the operator == returns true because, since both variable a and b are allocated a similar String value. The compiler will store a single copy of that value. Meaning that both variable a and b will point to the same address/memory location. This feature is called "interning". When you declared String a = new String("lemur") and String b = new String("lemur"). The compiler creates two objects that are stored in separate memory locations. So of course the == will return false. What the equals() method does is. It compares the value stored by two objects irrelevant of their memory location. Hope this makes it easier to remember the difference between the two.

alanabela
Автор

Not me binge-watching your content the past week because I'm supposed to be leading our Robotics Code Team in Java and I've never worked with Java before... thank you!!

riverpease
Автор

I had this exact question the day before this vid came out. Thanks so much for using ur Big Brain to help us :D

akshayburra
Автор

Thank you so much Alex! I've been binge watching all your tutorials, you've helped me more than my own teacher at university xp

ellineangelica
Автор

This was so helpful. I was stuck on a comparison issue for three hateful hours.

christianmoray
Автор

String literals are also String objects. But they're pooled/cached, so if you use the same literal twice, it uses the same object. Happens for performance reasons, but you can emulate it with any String using the intern() method.

Also you should perhaps mention that you rarely ever create Strings using the constructor yourself, instead you mostly get them from reading files, console input etc. Still needs equals()

JohnnyJayJay
Автор

I learnt so much from you about java❤️💕

hammadmughal
Автор

that's really helpful i was just studying about that and your video explained it better than "Introduction to Java Programming"

israasaad
Автор

Thank you. My mind got the clear concept.

AbhinavKumar-dref
Автор

That bonus tip is very helpful, thank you dude!

mouhamadzabaneh
Автор

Alex you are a good tutor, keep uploading new videos, don't worry i am daily at your videos, make easy videos and tutorials for evrything of java and i mean everything
LOL

thehiguiofficial
Автор

Thank you so much, You really helped me a lot with your tutorials. Godspeed!

jejester
Автор

❤❤❤I love your channel. You’re awesome

ilincsh
Автор

Alex you really make great tutorials!🌼
It would be great if you could also cover wrapper class

anuja
Автор

Alex, you're awesome! Thank you sm for teaching me how to use Java.

kevinhyden
Автор

Thanks a lot! This was exactly what I needed when I needed it.

andrewherrera
Автор

When you realize youtubers explain way more better than University proffessor

oro_doc
Автор

You should totally do a video on explaining generics.

angietimokhina
Автор

thank u so much that was super helpful🙏

abulkadersaid
Автор

Really useful, thanks. I'd love to see a fuller video on logical tests of processed strings in Java. I had some crazy results when casting an integer to a String and trying to verify its value by comparison to a string literal. Same when a substring is compared with a literal. As someone more used to wonderfully loose and automatically coerced javascript, the pedantry of java is a pain!

batchrocketproject