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

Поделиться
HTML-код
  • Опубликовано: 29 янв 2021
  • Complete Java course: codingwithjohn.thinkific.com/...
    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
    amzn.to/36AfdUu
    📕 One of my favorite programming books, Clean Code by Robert Martin
    amzn.to/3GTPVhf
    🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
    www.audibletrial.com/johnclean...
    🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
    bit.ly/3QPNGko
    📹Phone I use for recording:
    amzn.to/3HepYJu
    🎙️Microphone I use (classy, I know):
    amzn.to/3AYGdbz
    Donate with PayPal (Thank you so much!)
    www.paypal.com/donate/?hosted...
    ☕Complete Java course:
    codingwithjohn.thinkific.com/...
    codingwithjohn.com
  • НаукаНаука

Комментарии • 249

  • @rykageee
    @rykageee 3 года назад +137

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

  • @Daresigmally
    @Daresigmally Год назад +23

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

  • @cjm101
    @cjm101 Год назад +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!

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

  • @streamity24
    @streamity24 2 года назад +6

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

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

  • @olivierbegassat851
    @olivierbegassat851 11 месяцев назад +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 🙂

  • @magicalhats
    @magicalhats 2 года назад +10

    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.

  • @terrypark3690
    @terrypark3690 7 месяцев назад

    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.

  • @ovey2214
    @ovey2214 2 года назад +7

    That was incredibly well explained! Thanks a lot!

  • @dotanon
    @dotanon Год назад +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.

  • @manojpal4415
    @manojpal4415 Год назад

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

  • @WildNature170
    @WildNature170 Год назад

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

  • @theFUNkaarasChannel
    @theFUNkaarasChannel 2 года назад

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

  • @jesusromero71
    @jesusromero71 2 года назад +2

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

  • @michael_scarn_
    @michael_scarn_ Год назад

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

  • @Fawstah
    @Fawstah Год назад

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

  • @elijahasefa389
    @elijahasefa389 2 года назад +1

    this channel always dropping gems. Respect.

  • @shubhamkatwale6716
    @shubhamkatwale6716 2 года назад +1

    Very well explained Jonh, Thanks again ♥

  • @wayneinteressierts9973
    @wayneinteressierts9973 Год назад

    Great video as always. Thank you for your dedication!

  • @colonelh.s.l.3834
    @colonelh.s.l.3834 Год назад +1

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

  • @rahul64410
    @rahul64410 2 года назад

    Thanks for the video john seems like refreshing my basics

  • @praveens9233
    @praveens9233 Год назад +17

    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

  • @DatCrossGuy
    @DatCrossGuy Год назад

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

  • @dhavewala
    @dhavewala 2 года назад +20

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

  • @strigidae8939
    @strigidae8939 2 года назад

    Love your videos bro. Very helpful. Thanks a lot

  • @harlockgaming1476
    @harlockgaming1476 7 месяцев назад

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

  • @cananozdemir8913
    @cananozdemir8913 5 месяцев назад

    I just bought his full Java course. Amazing explanation .

  • @GenjaOrigins
    @GenjaOrigins 2 года назад +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

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

  • @Eduardado
    @Eduardado 2 года назад +1

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

  • @tonnytrumpet734
    @tonnytrumpet734 Месяц назад

    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.

  • @amaralensheriff
    @amaralensheriff Год назад

    Thanks John, your tutorials are awesome.

  • @thalathotitharunprabhakar3390
    @thalathotitharunprabhakar3390 2 года назад

    Easy to understand. Thank you.

  • @adamlasry5225
    @adamlasry5225 2 года назад

    Great lessons as usual. Thanks.

  • @ruzi173
    @ruzi173 10 месяцев назад

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

  • @technicalboy1816
    @technicalboy1816 2 года назад

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

  • @LTZMSYAL
    @LTZMSYAL Год назад

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

  • @ujjwals.dadwal8911
    @ujjwals.dadwal8911 2 года назад

    Thanks John, Mucha Gracias from Canada!

  • @shama-_-
    @shama-_- Год назад

    thanks a lot you help me to understand the concepts

  • @scottcarter4526
    @scottcarter4526 Год назад +1

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

  • @InfiniteWisdom1.0
    @InfiniteWisdom1.0 7 месяцев назад

    Thank you so much, your videos are pretty helpful 😊

  • @user-dm8tm2pm9o
    @user-dm8tm2pm9o 10 месяцев назад

    Thx for your clear presentation Sir

  • @surkesden
    @surkesden 2 года назад +1

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

  • @cestmoi5248
    @cestmoi5248 2 года назад

    wow, very well explained. Thank you

  • @bhagyakavijan9014
    @bhagyakavijan9014 Год назад

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

  • @lararifai7074
    @lararifai7074 2 года назад +1

    Very interesting ,thank you .

  • @eshwarnag
    @eshwarnag 8 месяцев назад

    the last part was really good!

  • @enfieldli9296
    @enfieldli9296 2 года назад

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

  • @mdemranalamin166
    @mdemranalamin166 2 года назад

    thank you, sir. !! great explanation.

  • @danieldanian8070
    @danieldanian8070 Год назад

    This guy is the best as a learner and teacher guide

  • @robertod893
    @robertod893 11 месяцев назад

    Excellent video as always!

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

  • @micahrufsvold
    @micahrufsvold 2 года назад +2

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

  • @YusufDemir-gx7he
    @YusufDemir-gx7he 2 года назад

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

  • @laurentlefevre9233
    @laurentlefevre9233 2 года назад

    Nice job John !

  • @ayanokoji8456
    @ayanokoji8456 Год назад

    Wow thanks man it is so well explain !

  • @altamashsabri8142
    @altamashsabri8142 Год назад

    Amazing explanation, thanks alot

  • @anjelomanoranjan
    @anjelomanoranjan 10 месяцев назад

    Thanks! Your videos are great!

  • @aymengouja3895
    @aymengouja3895 Год назад

    thanks a lot !!! awesome explanation

  • @stacywen7908
    @stacywen7908 Год назад

    thank you so much! very helpful!

  • @brtkgromek
    @brtkgromek 2 года назад +3

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

  • @dolevlevi7826
    @dolevlevi7826 2 года назад

    love your videos man !

  • @kilian9758
    @kilian9758 7 месяцев назад

    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?

  • @dylangovender
    @dylangovender 2 года назад

    Amazing tutorial!

  • @akna1987
    @akna1987 4 месяца назад

    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

  • @findlestick
    @findlestick 2 года назад

    This is a very useful video.

  • @9891254408
    @9891254408 Год назад

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

  • @saranr4878
    @saranr4878 Год назад

    Just the greatest explanation !

  • @ThePiko69
    @ThePiko69 2 года назад

    Great lesson :)

  • @lamaspacos
    @lamaspacos 4 месяца назад

    This channel is very very good.

  • @Coccoutube
    @Coccoutube Год назад

    Thanks John!

  • @konstantinzakharov5643
    @konstantinzakharov5643 11 месяцев назад

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

  • @HR-pz7ts
    @HR-pz7ts 10 месяцев назад

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

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

  • @pedroaboffa
    @pedroaboffa 2 года назад

    Thank you john!!

  • @glenn8781
    @glenn8781 2 года назад

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

  • @potatofypotatofy7772
    @potatofypotatofy7772 Год назад

    Thank you John

  • @NikolayTach
    @NikolayTach 2 года назад

    Good stuff brotherly.

  • @alyankhan806
    @alyankhan806 Год назад

    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❤

  • @HackInTheBoxx
    @HackInTheBoxx 2 года назад +1

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

  • @TeluguAbbai802
    @TeluguAbbai802 2 года назад

    Excellent teacher

  • @adeebalaifan8684
    @adeebalaifan8684 Год назад

    Perfect concept

  • @AHMED-ry2gy
    @AHMED-ry2gy 2 года назад

    dude, your videos are amazing

    • @pankajthorat4606
      @pankajthorat4606 2 года назад

      I am going through you video and its very easy to understand . One more request can you please provide session for Stack vs heap memory.

  • @hba6018
    @hba6018 2 года назад

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

  • @bellum_7997
    @bellum_7997 2 года назад

    nice one John :)

  • @galirams9653
    @galirams9653 2 месяца назад

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

  • @InfoBuzz1130
    @InfoBuzz1130 Год назад +1

    Hey pls make video on comparable comparator interface in java

  • @marioathan99
    @marioathan99 2 года назад

    Thank you so much

  • @user-qw9gr4cb8z
    @user-qw9gr4cb8z Год назад

    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 ❤

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

  • @MarioVillamizar
    @MarioVillamizar Год назад

    Cristal clear!

  • @thegloriousgoldeneagle
    @thegloriousgoldeneagle 6 месяцев назад

    I love your videos, thank you

  • @anubhavsingh2916
    @anubhavsingh2916 Год назад

    To the point explanation

  • @bextrekks8354
    @bextrekks8354 2 года назад

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

  • @borisvasilev4893
    @borisvasilev4893 Год назад

    Really Really cool!

  • @harshabhrito
    @harshabhrito Месяц назад

    Thank you sir.

  • @cryptoInNutshellR
    @cryptoInNutshellR Год назад

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