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

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ม.ค. 2025

ความคิดเห็น • 259

  • @rykageee
    @rykageee 3 ปีที่แล้ว +154

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

  • @Dare24
    @Dare24 2 ปีที่แล้ว +26

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

  • @magicalhats
    @magicalhats 2 ปีที่แล้ว +13

    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.

    • @alexanderpanday
      @alexanderpanday 8 หลายเดือนก่อน

      go away bootcamp people

  • @cjm101
    @cjm101 2 ปีที่แล้ว +4

    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!

  • @dotanon
    @dotanon 2 ปีที่แล้ว +2

    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.

  • @aureliasam2092
    @aureliasam2092 2 ปีที่แล้ว +1

    Hi John you did very well in this lesson. I tried to learn .equals and == from a professor but I couldn't understand it and now I got the point from you in 5 minutes.

  • @Visionist
    @Visionist 3 ปีที่แล้ว +16

    Simple and easy to understand. Thank you!

  • @iruga7379
    @iruga7379 2 ปีที่แล้ว +1

    OMFG please keep your content comming...im having a blast learning from you man. Im deeply grateful for all your lessons.

  • @streamity24
    @streamity24 2 ปีที่แล้ว +6

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

  • @terrypark3690
    @terrypark3690 ปีที่แล้ว

    wow this goes well with your string pool video. Thanks for making these really comprehensive videos on how java works for the basic things we just usually use without thinking about.

  • @theFUNkaarasChannel
    @theFUNkaarasChannel 2 ปีที่แล้ว

    I just found your channel and the world just makes so much more sense now.

  • @micahrufsvold
    @micahrufsvold 2 ปีที่แล้ว +4

    Small piece of feedback: title shoulda been ".equals() and == are not equal"
    Thanks for the great video!

  • @brtkgromek
    @brtkgromek 3 ปีที่แล้ว +6

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

  • @olivierbegassat851
    @olivierbegassat851 ปีที่แล้ว +2

    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 🙂

  • @dhavewala
    @dhavewala 3 ปีที่แล้ว +20

    John, this was an amazing tutorial!
    I had a query though; on the objects, do we override the equals() or is it inferred?

  • @ovey2214
    @ovey2214 2 ปีที่แล้ว +7

    That was incredibly well explained! Thanks a lot!

  • @tonnytrumpet734
    @tonnytrumpet734 6 หลายเดือนก่อน

    Sir I have been watching your videos for around 30 hours last 3 days, thank you so much this is great content I am starting to actually understand Java and see how things from different videos relate to each other, sometimes I can guess what you are going into with your train of thoughts, very well put together videos.

  • @harlockgaming1476
    @harlockgaming1476 ปีที่แล้ว

    Love the Kramer picture in the back, my grandpa has one just like it! Thanks for the amazing videos!

  • @praveens9233
    @praveens9233 ปีที่แล้ว +19

    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

  • @Fawstah
    @Fawstah 2 ปีที่แล้ว

    This actually helped me understand why some of the code I was writing was initially breaking. Ty!

  • @manojpal4415
    @manojpal4415 2 ปีที่แล้ว

    Had been scratching my head on a case similar to the example at last, luckily this video explained it. Huge thanks :)

  • @cananozdemir8913
    @cananozdemir8913 11 หลายเดือนก่อน

    I just bought his full Java course. Amazing explanation .

  • @danieldanian8070
    @danieldanian8070 2 ปีที่แล้ว

    This guy is the best as a learner and teacher guide

  • @WildNature170
    @WildNature170 2 ปีที่แล้ว

    Ah Lord! can't just say how easy you explained it. I have been really confused for several days about this simple .equals in object class and now I know how java processes it. Thank you so much for this video. Subscribed liked and commented. Keep up with your good work!!!!

  • @BelayeAssefa1
    @BelayeAssefa1 3 ปีที่แล้ว +1

    this channel always dropping gems. Respect.

  • @enfieldli9296
    @enfieldli9296 2 ปีที่แล้ว

    Wow, noob here, didnt know those details till you explain it, massive thanks John!

  • @xrecovery9
    @xrecovery9 4 วันที่ผ่านมา

    Thank you so much! You solved a bunch of hurdles I was facing while coding in Java. You earned a new Subscriber! By the way, what program are you using to code?

  • @brunomartel4639
    @brunomartel4639 ปีที่แล้ว +1

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

  • @jesusromero71
    @jesusromero71 2 ปีที่แล้ว +2

    tus videos tienen un gran valor, muchas gracias por compartir tu conocimiento

  • @DanielOliveira-ru7mt
    @DanielOliveira-ru7mt 2 ปีที่แล้ว +48

    Thanks Johnny Sins

    • @lyrixvibz
      @lyrixvibz 6 หลายเดือนก่อน +1

      Please be polite with your words👍

    • @1blkvinci
      @1blkvinci 5 หลายเดือนก่อน +1

      @@lyrixvibzhe said thank you

    • @lyrixvibz
      @lyrixvibz 5 หลายเดือนก่อน +1

      @@1blkvincido you know Johnny sins?💀

    • @SifatUllah-12
      @SifatUllah-12 5 หลายเดือนก่อน

      ​@@1blkvinciThat's not the right way to say thank you.

  • @HR-pz7ts
    @HR-pz7ts ปีที่แล้ว

    Once you learn a thing or two about oops you know exactly what's going on. Thank you for the video

  • @DatCrossGuy
    @DatCrossGuy 2 ปีที่แล้ว

    Thanks so much for the video. Genuinely looking forward to the "how to write a good equals method" vid!

  • @the_Spartan_117
    @the_Spartan_117 ปีที่แล้ว

    By the beard of Zeus! Your contributions to the world of programming are nothing short of epic. Your Java beginner's playlist is truly a stroke of genius, and I speak for many when I say, you are the real MVP!

  • @michael_scarn_
    @michael_scarn_ ปีที่แล้ว

    Thanks John, your tutorials are very helpful. You are a great teacher, hats off!

  • @LTZMSYAL
    @LTZMSYAL 2 ปีที่แล้ว

    Thansk's John. Please don't stop your videos they are awesome

  • @akna1987
    @akna1987 10 หลายเดือนก่อน

    These videos are great! But you should have included a video on how to implement the .equals-method and compare-to method to your own class

  • @scottcarter4526
    @scottcarter4526 2 ปีที่แล้ว +1

    Might also be worth mentioning that you can use == with enum's as well.

  • @pawehericht2673
    @pawehericht2673 6 หลายเดือนก่อน

    at 1:46 it is worth saying, that
    String one = "1";
    String oneBis = "1";
    when comparing one == oneBis will output true? what has changed and why
    String one = new String("1) gives us false when ==?
    The answer is: The String pool I believe

  • @danielcommins1285
    @danielcommins1285 2 ปีที่แล้ว +2

    John, where did you get the source and how did you attach the source to the binaries? UPDATE: solution is to point your JRE path to the path with the JDK instead!

  • @ruzi173
    @ruzi173 ปีที่แล้ว

    This is just the best explanation..wow👏Thank you for all your videos🙏

  • @technicalboy1816
    @technicalboy1816 3 ปีที่แล้ว

    I wish you were my lecturer at Uni. You have talent man!

  • @9891254408
    @9891254408 2 ปีที่แล้ว

    This is the best i have understoon so far...

  • @SecretFols
    @SecretFols ปีที่แล้ว

    The underline is even if the primitive or non primitive or user defined type java will check for the memory location and not the values.. (if the values are same then uses same memory location).

  • @Amy-zb4iq
    @Amy-zb4iq 2 ปีที่แล้ว +1

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

  • @amaralensheriff
    @amaralensheriff ปีที่แล้ว

    Thanks John, your tutorials are awesome.

  • @lamaspacos
    @lamaspacos 9 หลายเดือนก่อน

    This channel is very very good.

  • @nilou-s8l
    @nilou-s8l 2 ปีที่แล้ว

    You explained sth in 8minutes that my French prof couldn’t explain in 2 hours and made me hate Java for no reason . Thank you ❤

  • @rahul64410
    @rahul64410 3 ปีที่แล้ว

    Thanks for the video john seems like refreshing my basics

  • @ujjwals.dadwal8911
    @ujjwals.dadwal8911 2 ปีที่แล้ว

    Thanks John, Mucha Gracias from Canada!

  • @InfoBuzz1130
    @InfoBuzz1130 2 ปีที่แล้ว +1

    Hey pls make video on comparable comparator interface in java

  • @cryptoInNutshellR
    @cryptoInNutshellR ปีที่แล้ว

    Thanks. Useful, however you still did not cover how to compare 2 objects on the Dog example staring from 05:08

  • @eshwarnag
    @eshwarnag ปีที่แล้ว

    the last part was really good!

  • @robertod893
    @robertod893 ปีที่แล้ว

    Excellent video as always!

  • @galirams9653
    @galirams9653 7 หลายเดือนก่อน

    John, please clarify this for me, in text, how would you declare and compare 2 string objects for equality?

  • @shubhamkatwale6716
    @shubhamkatwale6716 3 ปีที่แล้ว +1

    Very well explained Jonh, Thanks again ♥

  • @Eduardado
    @Eduardado 2 ปีที่แล้ว +1

    Very clear. Awesome examples and also my eyes love the dark theme ^^

  • @YusufDemir-gx7he
    @YusufDemir-gx7he 2 ปีที่แล้ว

    It seems I've found my coding channel. Thanks for the video :)

  • @علیحمیدی-س2ر
    @علیحمیدی-س2ر ปีที่แล้ว

    Thx for your clear presentation Sir

  • @MasonJF
    @MasonJF 2 ปีที่แล้ว +1

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

  • @Zalaniar
    @Zalaniar 2 ปีที่แล้ว

    I was gonna say something about the weird use of a string constructor at the beginning, but it's a good thing I decided to wait, lol. IIRC, the javadoc even yells at people about wanting to use == for strings which I find hilarious.

  • @iyhan1987
    @iyhan1987 2 ปีที่แล้ว +1

    7:03 I guess almost everyone(who studies Java seriously) knows about the pool of strings cause it is a pretty common topic. I bet if you want really “to impress some nerds” you should show them some “pool of Integer” style tricks :)

  • @colonelh.s.l.3834
    @colonelh.s.l.3834 2 ปีที่แล้ว +1

    This is awesome! Can you do a video on priority queues and comparators, and how they come together?

  • @binilgeorge7358
    @binilgeorge7358 2 ปีที่แล้ว +2

    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.

    • @lumschente
      @lumschente 2 ปีที่แล้ว

      the string itself is probably wrapped in a class and the class stores the reference to the string pool but the objects are initialized in different memory regions

    • @binilgeorge7358
      @binilgeorge7358 2 ปีที่แล้ว

      @@lumschente Don't know if it is true, but that makes sense

    • @shravanbandari
      @shravanbandari 2 ปีที่แล้ว

      I was thinking about the same, @john could you please clarify this question
      Here is the other video of your we were referring to 🙂
      th-cam.com/video/Bj9Mx_Lx3q4/w-d-xo.html

  • @hba6018
    @hba6018 2 ปีที่แล้ว

    String Pool, the place where are inline strings are saved, that's why you use new String("hello")

  • @shama-_-
    @shama-_- 2 ปีที่แล้ว

    thanks a lot you help me to understand the concepts

  • @konstantinzakharov5643
    @konstantinzakharov5643 ปีที่แล้ว

    I love this guy! I am hitting the Like button!

  • @alyankhan806
    @alyankhan806 2 ปีที่แล้ว

    The API contract also states that if you override equals() then you must override the hashcode(), it's an important detail!

  • @bhukyasharath2015
    @bhukyasharath2015 ปีที่แล้ว +1

    Superb sir❤

  • @surkesden
    @surkesden 2 ปีที่แล้ว +1

    Good explanation! What about enum comparation? What should I use?

  • @wayneinteressierts9973
    @wayneinteressierts9973 ปีที่แล้ว

    Great video as always. Thank you for your dedication!

  • @InfiniteWisdom1.0
    @InfiniteWisdom1.0 ปีที่แล้ว

    Thank you so much, your videos are pretty helpful 😊

  • @laurentlefevre9233
    @laurentlefevre9233 2 ปีที่แล้ว

    Nice job John !

  • @saranr4878
    @saranr4878 2 ปีที่แล้ว

    Just the greatest explanation !

  • @bhagyakavijan9014
    @bhagyakavijan9014 2 ปีที่แล้ว

    Thank you so much.. Your explanation is very clear...!!!

  • @GenjaOrigins
    @GenjaOrigins 3 ปีที่แล้ว +3

    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, exceptions/throw/finally/catch 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

  • @ayanokoji8456
    @ayanokoji8456 2 ปีที่แล้ว

    Wow thanks man it is so well explain !

  • @kilian9758
    @kilian9758 ปีที่แล้ว

    Your tutorials are awesome! Though I wonder if this affects switch cases too. Can I always use switch cases or do I need to be cautious just like with the == operator?

  • @HackInTheBoxx
    @HackInTheBoxx 3 ปีที่แล้ว +1

    Can you do a video sometime on instanceof, thank you.

  • @JPS13Laptop
    @JPS13Laptop 3 ปีที่แล้ว +1

    As a python programmer, I definitely see myself getting confused if I were to ever get into Java. Thanks for the explanation :)

    • @tazogochitashvili6514
      @tazogochitashvili6514 3 ปีที่แล้ว +1

      You shouldn't look too deep into the difference between "is" and "=" in python then lol

    • @JPS13Laptop
      @JPS13Laptop 3 ปีที่แล้ว

      @@tazogochitashvili6514 I actually do realise the difference between the two. But I don't use 'is' in my own code tbh. Also it annoys me that Java uses == for object comparison whereas Python uses it for value comparison.

    • @palmolive4541
      @palmolive4541 2 ปีที่แล้ว +6

      @@JPS13Laptop Java doesn’t use == for object comparison, that was the whole point of this video

    • @JPS13Laptop
      @JPS13Laptop 2 ปีที่แล้ว

      @@palmolive4541 It appears that I still don't fully grasp what an object is. Also it seems that `is` in Python is what `==` does in Java, which is still annoying tbh.

    • @tazogochitashvili6514
      @tazogochitashvili6514 2 ปีที่แล้ว +1

      @@JPS13Laptop that means that you don't understand either "is" in python or "==" in Java

  • @thalathotitharunprabhakar3390
    @thalathotitharunprabhakar3390 2 ปีที่แล้ว

    Easy to understand. Thank you.

  • @anubhavsingh2916
    @anubhavsingh2916 ปีที่แล้ว

    To the point explanation

  • @GaryChike
    @GaryChike 2 ปีที่แล้ว

    This is a gotcha for those coming from C#. Great explanation John!

  • @altamashsabri8142
    @altamashsabri8142 2 ปีที่แล้ว

    Amazing explanation, thanks alot

  • @LAZIO14
    @LAZIO14 ปีที่แล้ว

    Thanks! Your videos are great!

  • @glenn8781
    @glenn8781 2 ปีที่แล้ว

    Can you please make that video you promised on writing a good equals method?

  • @bextrekks8354
    @bextrekks8354 2 ปีที่แล้ว

    lmao, that head pop up at the beginning cracks me up!

  • @HIMANSHURAGHAV-km8rr
    @HIMANSHURAGHAV-km8rr 4 หลายเดือนก่อน

    Thanks john 😄

  • @manishvashishth2663
    @manishvashishth2663 2 ปีที่แล้ว

    What about StringBuilder & StringBuffer ??

  • @amruthashekar3544
    @amruthashekar3544 5 หลายเดือนก่อน

    Hello John, please answer why i am getting false in first case?
    String str1 = new String("hello");
    String str2 = new String("hello");
    System.out.println(str1.equals(str2));
    and
    String str = new String("java");
    String rev = new String("avaj");
    System.out.println(str.equals(rev));
    how string comparision is done, is it character by character?

    • @SupunWithana-t8x
      @SupunWithana-t8x 4 หลายเดือนก่อน

      I think better you recheck 1st case, it has to be true

  • @simonjunior184
    @simonjunior184 3 ปีที่แล้ว +7

    Amazing explanation. My Uni java lecturer even didn't taught us this important concept. Keep up the great job!

  • @adamlasry5225
    @adamlasry5225 2 ปีที่แล้ว

    Great lessons as usual. Thanks.

  • @potatofypotatofy7772
    @potatofypotatofy7772 2 ปีที่แล้ว

    Thank you John

  • @Coccoutube
    @Coccoutube 2 ปีที่แล้ว

    Thanks John!

  • @matteoZattera
    @matteoZattera 2 ปีที่แล้ว

    Why the equals method inside Dog class take an Object type parameter instead of a Dog type parameter?

  • @BEN-ys6gu
    @BEN-ys6gu 2 ปีที่แล้ว

    I don't do Java, but what about enumerations? If they exist does the == work?

    • @LichtHund
      @LichtHund 2 ปีที่แล้ว

      Yes, for enums you also use ==

  • @aymengouja3895
    @aymengouja3895 2 ปีที่แล้ว

    thanks a lot !!! awesome explanation

  • @strigidae8939
    @strigidae8939 2 ปีที่แล้ว

    Love your videos bro. Very helpful. Thanks a lot

  • @dylangovender
    @dylangovender 2 ปีที่แล้ว

    Amazing tutorial!

  • @viv7531
    @viv7531 2 ปีที่แล้ว

    Hello John, after You Ctrl + click on .equals (4:15 min) how did you go back? I mean how to hide all that info that appeared after Ctrl + clicking?

    • @matteoZattera
      @matteoZattera 2 ปีที่แล้ว

      It just opens the file where .equals is defined, just close this file

  • @cestmoi5248
    @cestmoi5248 2 ปีที่แล้ว

    wow, very well explained. Thank you