Java String Comparison Tutorial (Equals vs == in Java)
Вставка
- Опубліковано 19 сер 2020
- $1,000 OFF ANY Springboard Tech Bootcamps with my code ALEXLEE. See if you qualify for the JOB GUARANTEE! 👉 bit.ly/3HX970h
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!
Full Java Tutorial For Beginners Playlist: • Full Java Course by Al...
Free Tips: bit.ly/3U6HXcb
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
If you’re new to programming but want a career in tech, I HIGHLY RECOMMEND applying to one of Springboard’s online coding bootcamps (use code ALEXLEE for $1,000 off): bit.ly/3HX970h
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.
There's also a "===" comparator in other languages which I presume checks the contents of the objects (if both are of the same class, and their respective fields' values are equal), but it probably hasn't been introduced into Java to avoid further confusion.
Thank you sir.
@@mr.rabbit5642 if you're talking about JavaScript, the difference between "==" and "===" is that "==" isn't type safe (3 == "3") but "===" is type-safe (3 !== "3" but 3 === 3). In other words, "==" attempts to "coerce" the types to be equal.
great explanation man thanks a lot!
Great explanation 👍
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
Congratulations on 93k subs!
It's over 174k
I had this exact question the day before this vid came out. Thanks so much for using ur Big Brain to help us :D
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!!
How did u do?
I learnt so much from you about java❤️💕
That bonus tip is very helpful, thank you dude!
Thank you so much, You really helped me a lot with your tutorials. Godspeed!
Thanks a lot! This was exactly what I needed when I needed it.
Alex you really make great tutorials!🌼
It would be great if you could also cover wrapper class
Road to 100k! Keep it up Alex!
Your channel is so good dude
This was so helpful. I was stuck on a comparison issue for three hateful hours.
that's really helpful i was just studying about that and your video explained it better than "Introduction to Java Programming"
Thanks
I needed this
Alex, you're awesome! Thank you sm for teaching me how to use Java.
thank you. This was helpful.
Thank you. My mind got the clear concept.
Dude excellent explanation!
thank u so much that was super helpful🙏
thanks for this informative Tutorial
Thanks, really helped me.
thank you,keep doing more
Thanks for nice solution
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()
Very helpful. Thank you.
Thank you so much!
Thank You very much.
Thanks a lot bro!!
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
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!
You should explain about memory allocation for this for better understanding. Like heap and string pool.
Alex I love you as a brother☺❤
THANK YOU
You should totally do a video on explaining generics.
When you realize youtubers explain way more better than University proffessor
but it makes sense if you think the university professors job it is to teach in the most correct way possible while the youtubers "job" is to explain it in the simplest way possible
Can you do a tutorial on hashcode method? They say it almost always go with the equals method.
I really enjoyed the video!
Can you make a Java Calculator program in Eclipse? I am having problems with it.
Thanks dude
Hello, thank you for the video ! I wanted to ask if you can give me tips on how to do this but in a recursive function ? I am having a hard time doing it
Can you make a video on using rhe equals method and hashcode
Thanks for the great videos 📹
Could you please make a video on overloading vs overriding ?
i have a question but it is not reffering to java - Which video screen capture are you using ? and how do you do that thing that i see your screen and a little screen of you on buttom ?
hey nice video, can you make a video about annotations?
is equals method inside the String class or any non-primitive type can use it ?
Hey! How can I search for a part of a string in an array or text file?
So just putting in "ong" should recognize the whole Object "Happy Song".
Thanks prior for the answer!
Can you please do a video on arrays of objects
Thank You Bro
What is the outro song??? Please tell me someone.
you are amazing
To check if variable is null, use (string == null) instead of equals, contains or equalsIgnoreCase.
Great.
l know this is unrelated to the video but how can you put assets in Java?
What ide do you use?
this guy is helping me get a degree. i will pay some day
i fucking love you bro i was stuck on this shit for ;like 2 days
When you String a = "lemur"; java records "a" in its heap and register it to a fixed size location in memory which has the values "lemur", java stores any String object in a string pool so no 2 same string will have different memory location address. Then the == operator compares the two location address and returns true or false accordingly.
The "new" keyword is used when constructing an instance of a class, using it twice would create two new objects with different location addresses.
the .equals method is a method of the Comparator Interface. String class implements Comparator and gave it a concreate method that compares each character in the objects sequence.
man i hope im right.
How to do with scannee calss
Is it true that a String in java is just an array of characters?
couldn't you use boolean somehow instead of typing an output for true and false?
where is the link ?
5:40 : u can use [String].equalsIgnoreCase([String]); :)
Morning sensei
Hey, can you please start Advance java as well.
You should make a video on parsing json from a http address
instead of using toLowerCase then comparing it to a string, you can just use equalsIgnoreCase
I want to learn basic programming for beginners thank and I hope
I wonder why you are not using IntelliJ?
Please please please one video about composition 💔💔😢
Maybe you would sell more merch if you change the slogan/ catchy phrase
Noone wants to wear “I have no idea what I’m doing” in public 😅
Ever considered use IntelliJ idea?
@Sticklink yup
0:23
"Strings orphan"
Notif Squad
Bro, you got wrong here as Constructor is used to initialise the object "new" keyword is used to make object!
Do you now ? You are amzing 💖
However, writing another program listed below, resembling to you , I obtain a different result, that is true from it; however, yours is false. Why?
import java.util.Scanner;
public class tut4_8 {
public static void main(String[]args) {
String a= new String("SYNCO");
String b= new String("SYNCO");
System.out.println(a.equals(b));
}
}
hay man, can you make a tutorial to make a calculator
I wonder what the devs were smoking when they designed strings in java
whats wrong with strings in java?
im praying you hit the dark mode button
System.out.println("Great video!!!");
☕☕☕
please reply what you think about India ,I am with you from 5002 sub
I want to do free Java Programming Job.
It’s not confusing your “explanation” is confusing. Actually you’re not even explaining anything here.
Guys remember:
1. == checks for reference (if we are pointing to the same object than of course it’s true if not than we have 2 objects that need additional comparison therefore
2. Use equals to compare objects based on their properties (id, name, phone number etc)
And yes
3. == is used for comparing the equality between primitive types
You know the difference, but you don’t know why the difference, you wasted my time