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

КОМЕНТАРІ • 98

  • @alexlorenlee
    @alexlorenlee  7 місяців тому +1

    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

  • @alanabela9536
    @alanabela9536 3 роки тому +141

    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.

    • @mr.rabbit5642
      @mr.rabbit5642 3 роки тому

      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.

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

      Thank you sir.

    • @NateROCKS112
      @NateROCKS112 2 роки тому +2

      @@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.

    • @hasanDalbudak
      @hasanDalbudak Рік тому +1

      great explanation man thanks a lot!

    • @cricketfunda2228
      @cricketfunda2228 Рік тому +1

      Great explanation 👍

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

    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

  • @user-zf8gy1yw1p
    @user-zf8gy1yw1p 3 роки тому +10

    Congratulations on 93k subs!

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

      It's over 174k

  • @akshayburra6629
    @akshayburra6629 3 роки тому +11

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

  • @riverpease6611
    @riverpease6611 2 роки тому +11

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

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

    I learnt so much from you about java❤️💕

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

    That bonus tip is very helpful, thank you dude!

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

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

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

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

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

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

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

    Road to 100k! Keep it up Alex!

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

    Your channel is so good dude

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

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

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

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

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

    Thanks
    I needed this

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

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

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

    thank you. This was helpful.

  • @AbhinavKumar-dr4ef
    @AbhinavKumar-dr4ef 2 роки тому

    Thank you. My mind got the clear concept.

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

    Dude excellent explanation!

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

    thank u so much that was super helpful🙏

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

    thanks for this informative Tutorial

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

    Thanks, really helped me.

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

    thank you,keep doing more

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

    Thanks for nice solution

  • @JohnnyJayJay
    @JohnnyJayJay 3 роки тому +7

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

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

    Very helpful. Thank you.

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

    Thank you so much!

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

    Thank You very much.

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

    Thanks a lot bro!!

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

    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

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

    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!

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

    You should explain about memory allocation for this for better understanding. Like heap and string pool.

  • @ahmedabdulkareem783
    @ahmedabdulkareem783 Рік тому +1

    Alex I love you as a brother☺❤

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

    THANK YOU

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

    You should totally do a video on explaining generics.

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

    When you realize youtubers explain way more better than University proffessor

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

      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

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

    Can you do a tutorial on hashcode method? They say it almost always go with the equals method.

  • @Admin-ts6op
    @Admin-ts6op 3 роки тому

    I really enjoyed the video!
    Can you make a Java Calculator program in Eclipse? I am having problems with it.

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

    Thanks dude

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

    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

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

    Can you make a video on using rhe equals method and hashcode
    Thanks for the great videos 📹

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

    Could you please make a video on overloading vs overriding ?

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

    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 ?

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

    hey nice video, can you make a video about annotations?

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

    is equals method inside the String class or any non-primitive type can use it ?

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

    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!

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

    Can you please do a video on arrays of objects

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

    Thank You Bro

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

    What is the outro song??? Please tell me someone.

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

    you are amazing

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

    To check if variable is null, use (string == null) instead of equals, contains or equalsIgnoreCase.

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

    Great.

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

    l know this is unrelated to the video but how can you put assets in Java?

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

    What ide do you use?

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

    this guy is helping me get a degree. i will pay some day

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

    i fucking love you bro i was stuck on this shit for ;like 2 days

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

    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.

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

    How to do with scannee calss

  • @JasonSmith-wg2eg
    @JasonSmith-wg2eg 2 роки тому

    Is it true that a String in java is just an array of characters?

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

    couldn't you use boolean somehow instead of typing an output for true and false?

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

    where is the link ?

  • @Phoenix-CM
    @Phoenix-CM 3 роки тому

    5:40 : u can use [String].equalsIgnoreCase([String]); :)

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

    Morning sensei

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

    Hey, can you please start Advance java as well.

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

    You should make a video on parsing json from a http address

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

    instead of using toLowerCase then comparing it to a string, you can just use equalsIgnoreCase

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

    I want to learn basic programming for beginners thank and I hope

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

    I wonder why you are not using IntelliJ?

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

    Please please please one video about composition 💔💔😢

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

    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 😅

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

    Ever considered use IntelliJ idea?

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

    0:23
    "Strings orphan"

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

    Notif Squad

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

    Bro, you got wrong here as Constructor is used to initialise the object "new" keyword is used to make object!

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

    Do you now ? You are amzing 💖

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

    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));
    }
    }

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

    hay man, can you make a tutorial to make a calculator

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

    I wonder what the devs were smoking when they designed strings in java

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

      whats wrong with strings in java?

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

    im praying you hit the dark mode button

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

    System.out.println("Great video!!!");

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

    ☕☕☕

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

    please reply what you think about India ,I am with you from 5002 sub

  • @FahimAhmed-iq7do
    @FahimAhmed-iq7do 3 роки тому

    I want to do free Java Programming Job.

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

    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

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

    You know the difference, but you don’t know why the difference, you wasted my time