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

  Рет қаралды 195,476

Coding with John

Coding with John

Күн бұрын

Пікірлер: 258
@rykageee
@rykageee 3 жыл бұрын
so far I've watched several videos with the same topic but this video only explained well. Thanks man!
@CodingWithJohn
@CodingWithJohn 3 жыл бұрын
Awesome! Very glad I was able to help!
@phillipmiller4642
@phillipmiller4642 5 ай бұрын
Agreed
@Dare24
@Dare24 2 жыл бұрын
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 жыл бұрын
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 6 ай бұрын
go away bootcamp people
@aureliasam2092
@aureliasam2092 2 жыл бұрын
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.
@cjm101
@cjm101 Жыл бұрын
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 Жыл бұрын
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.
@praveens9233
@praveens9233 Жыл бұрын
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
@Visionist
@Visionist 3 жыл бұрын
Simple and easy to understand. Thank you!
@theFUNkaarasChannel
@theFUNkaarasChannel 2 жыл бұрын
I just found your channel and the world just makes so much more sense now.
@brtkgromek
@brtkgromek 2 жыл бұрын
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! :)
@enigma6463
@enigma6463 2 жыл бұрын
😂😂
@iruga7379
@iruga7379 2 жыл бұрын
OMFG please keep your content comming...im having a blast learning from you man. Im deeply grateful for all your lessons.
@streamity24
@streamity24 2 жыл бұрын
Really fascinated at how good you are able to explain. Thanks for the great video!
@micahrufsvold
@micahrufsvold 2 жыл бұрын
Small piece of feedback: title shoulda been ".equals() and == are not equal" Thanks for the great video!
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
.equals() != ==
@micahrufsvold
@micahrufsvold 2 жыл бұрын
@@CodingWithJohn even better!
@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.
@danieldanian8070
@danieldanian8070 2 жыл бұрын
This guy is the best as a learner and teacher guide
@ovey2214
@ovey2214 2 жыл бұрын
That was incredibly well explained! Thanks a lot!
@cananozdemir8913
@cananozdemir8913 9 ай бұрын
I just bought his full Java course. Amazing explanation .
@Fawstah
@Fawstah Жыл бұрын
This actually helped me understand why some of the code I was writing was initially breaking. Ty!
@dhavewala
@dhavewala 2 жыл бұрын
John, this was an amazing tutorial! I had a query though; on the objects, do we override the equals() or is it inferred?
@wiktorstankiewicz3568
@wiktorstankiewicz3568 2 жыл бұрын
we override it
@fullfungo
@fullfungo 2 жыл бұрын
6:15
@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!
@manojpal4415
@manojpal4415 2 жыл бұрын
Had been scratching my head on a case similar to the example at last, luckily this video explained it. Huge thanks :)
@olivierbegassat851
@olivierbegassat851 Жыл бұрын
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 🙂
@tonnytrumpet734
@tonnytrumpet734 5 ай бұрын
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.
@enfieldli9296
@enfieldli9296 2 жыл бұрын
Wow, noob here, didnt know those details till you explain it, massive thanks John!
@harlockgaming1476
@harlockgaming1476 11 ай бұрын
Love the Kramer picture in the back, my grandpa has one just like it! Thanks for the amazing videos!
@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!!!!
@BelayeAssefa1
@BelayeAssefa1 3 жыл бұрын
this channel always dropping gems. Respect.
@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
@jesusromero71
@jesusromero71 2 жыл бұрын
tus videos tienen un gran valor, muchas gracias por compartir tu conocimiento
@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 ❤
@scottcarter4526
@scottcarter4526 2 жыл бұрын
Might also be worth mentioning that you can use == with enum's as well.
@DatCrossGuy
@DatCrossGuy 2 жыл бұрын
Thanks so much for the video. Genuinely looking forward to the "how to write a good equals method" vid!
@technicalboy1816
@technicalboy1816 2 жыл бұрын
I wish you were my lecturer at Uni. You have talent man!
@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
@ujjwals.dadwal8911
@ujjwals.dadwal8911 2 жыл бұрын
Thanks John, Mucha Gracias from Canada!
@pawehericht2673
@pawehericht2673 5 ай бұрын
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
@akna1987
@akna1987 9 ай бұрын
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
@danielcommins1285
@danielcommins1285 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!
@9891254408
@9891254408 2 жыл бұрын
This is the best i have understoon so far...
@YusufDemir-gx7he
@YusufDemir-gx7he 2 жыл бұрын
It seems I've found my coding channel. Thanks for the video :)
@MasonJF
@MasonJF 2 жыл бұрын
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!".
@ruzi173
@ruzi173 Жыл бұрын
This is just the best explanation..wow👏Thank you for all your videos🙏
@Eduardado
@Eduardado 2 жыл бұрын
Very clear. Awesome examples and also my eyes love the dark theme ^^
@brunomartel4639
@brunomartel4639 Жыл бұрын
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?.
@alyankhan806
@alyankhan806 2 жыл бұрын
The API contract also states that if you override equals() then you must override the hashcode(), it's an important detail!
@GenjaOrigins
@GenjaOrigins 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
@rahul64410
@rahul64410 2 жыл бұрын
Thanks for the video john seems like refreshing my basics
@shubhamkatwale6716
@shubhamkatwale6716 2 жыл бұрын
Very well explained Jonh, Thanks again ♥
@amaralensheriff
@amaralensheriff Жыл бұрын
Thanks John, your tutorials are awesome.
@eshwarnag
@eshwarnag Жыл бұрын
the last part was really good!
@hba6018
@hba6018 2 жыл бұрын
String Pool, the place where are inline strings are saved, that's why you use new String("hello")
@علیحمیدی-س2ر
@علیحمیدی-س2ر Жыл бұрын
Thx for your clear presentation Sir
@lamaspacos
@lamaspacos 8 ай бұрын
This channel is very very good.
@DanielOliveira-ru7mt
@DanielOliveira-ru7mt Жыл бұрын
Thanks Johnny Sins
@lyrixvibz
@lyrixvibz 5 ай бұрын
Please be polite with your words👍
@1blkvinci
@1blkvinci 4 ай бұрын
@@lyrixvibzhe said thank you
@lyrixvibz
@lyrixvibz 4 ай бұрын
@@1blkvincido you know Johnny sins?💀
@SifatUllah-12
@SifatUllah-12 3 ай бұрын
​@@1blkvinciThat's not the right way to say thank you.
@JPS13Laptop
@JPS13Laptop 2 жыл бұрын
As a python programmer, I definitely see myself getting confused if I were to ever get into Java. Thanks for the explanation :)
@tazogochitashvili6514
@tazogochitashvili6514 2 жыл бұрын
You shouldn't look too deep into the difference between "is" and "=" in python then lol
@JPS13Laptop
@JPS13Laptop 2 жыл бұрын
@@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 жыл бұрын
@@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 жыл бұрын
@@JPS13Laptop that means that you don't understand either "is" in python or "==" in Java
@Loghinesko
@Loghinesko Жыл бұрын
The "new" keyword means that when we use it to create an instance of a class, we tell the computer that we want to allocate memory space to put the new object in.... After that, we want to create a new object again in the class, so we need to use the "new" keyword again which again tells the computer that we want to allocate memory for the new object, so it allocates a new memory address to the new object. Right now, we have the 2 objects, but they have different memory addresses because they were instantiated in different memory areas. When we compare objects, we are actually comparing the memory addresses of variables (objects). In fact, these variables are pointers to objects.
@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).
@colonelh.s.l.3834
@colonelh.s.l.3834 Жыл бұрын
This is awesome! Can you do a video on priority queues and comparators, and how they come together?
@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.
@Zalaniar
@Zalaniar 2 жыл бұрын
In case anyone is worried about java doubling up on references to strings like that causing weird bugs, don't worry... because strings are immutable, if you try to change one, java will just make a new String object and point the variable to that one instead, leaving the first untouched.
@wayneinteressierts9973
@wayneinteressierts9973 Жыл бұрын
Great video as always. Thank you for your dedication!
@Amy-zb4iq
@Amy-zb4iq 2 жыл бұрын
What’s the codes for .equals() in class Dog?
@surkesden
@surkesden 2 жыл бұрын
Good explanation! What about enum comparation? What should I use?
@iyhan1987
@iyhan1987 2 жыл бұрын
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 :)
@ernestoc.d.1868
@ernestoc.d.1868 3 жыл бұрын
I would like to see the "How to implement a good .equals() method".
@inx1819
@inx1819 2 жыл бұрын
Step 1. Use Lombok
@wannabeserpento99
@wannabeserpento99 2 жыл бұрын
Eclipse can generate one for you and I don't know why he doesn't mention it
@Pacvalham
@Pacvalham 2 жыл бұрын
1. check for identity (if(this == obj) return true;) 2. check for type (if(!(obj instanceof _ThisClass_ )) return false;) 3.a cast the object ( _ThisClass_ castObj = ( _ThisClass_ ) obj;) 3.b compare each field (return this.a == _[or .equals()]_ castObj.a && this.b == _[or .equals()]_ castObj.b && ...;)
@shama-_-
@shama-_- Жыл бұрын
thanks a lot you help me to understand the concepts
@bhagyakavijan9014
@bhagyakavijan9014 2 жыл бұрын
Thank you so much.. Your explanation is very clear...!!!
@saranr4878
@saranr4878 2 жыл бұрын
Just the greatest explanation !
@simonjunior184
@simonjunior184 3 жыл бұрын
Amazing explanation. My Uni java lecturer even didn't taught us this important concept. Keep up the great job!
@thalathotitharunprabhakar3390
@thalathotitharunprabhakar3390 2 жыл бұрын
Easy to understand. Thank you.
@konstantinzakharov5643
@konstantinzakharov5643 Жыл бұрын
I love this guy! I am hitting the Like button!
@robertod893
@robertod893 Жыл бұрын
Excellent video as always!
@medkamy2393
@medkamy2393 Жыл бұрын
Best tuto ever
@provokator-provocateur7603
@provokator-provocateur7603 2 жыл бұрын
Best intro ever
@radhemohannaamhai
@radhemohannaamhai 2 жыл бұрын
Man they rejected me yesterday based on this question
@galirams9653
@galirams9653 6 ай бұрын
John, please clarify this for me, in text, how would you declare and compare 2 string objects for equality?
@GaryChike
@GaryChike 2 жыл бұрын
This is a gotcha for those coming from C#. Great explanation John!
@potatofypotatofy7772
@potatofypotatofy7772 Жыл бұрын
Thank you John
@InfoBuzz1130
@InfoBuzz1130 Жыл бұрын
Hey pls make video on comparable comparator interface in java
@Sui-Zan
@Sui-Zan 2 жыл бұрын
Orange is the new black, John. You look great! Thank you very much for the videos. You make java seem so simple to me.
@MarioVillamizar
@MarioVillamizar 2 жыл бұрын
Cristal clear!
@InfiniteWisdom1.0
@InfiniteWisdom1.0 11 ай бұрын
Thank you so much, your videos are pretty helpful 😊
@anubhavsingh2916
@anubhavsingh2916 Жыл бұрын
To the point explanation
@technoultimategaming2999
@technoultimategaming2999 2 жыл бұрын
And it's very intuitive why == cannot always work. If you have a Room class and you ask java to check if a room is equals to another room... Well... It doesn't know what to check, should it check dimensions of the room, should it check furniture inside? While with numbers it's very obvious what people mean when one number equals another, but for objects like rooms it's really up to the programer to define what "equals" actually means
@Coccoutube
@Coccoutube 2 жыл бұрын
Thanks John!
@laurentlefevre9233
@laurentlefevre9233 2 жыл бұрын
Nice job John !
@HackInTheBoxx
@HackInTheBoxx 2 жыл бұрын
Can you do a video sometime on instanceof, thank you.
@kilian9758
@kilian9758 11 ай бұрын
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?
@bhukyasharath2015
@bhukyasharath2015 Жыл бұрын
Superb sir❤
@barneylaurance1865
@barneylaurance1865 2 жыл бұрын
I would call `==` the identity operator. It tells us if the two things are identical, not just if they are equal. In the real world two mass produced objects, like two coffee cups might be equal in every aspect you can measure, but they are not identical because they are two distinct objects.
@Programming-for-all-u2l
@Programming-for-all-u2l 2 жыл бұрын
Excellent teacher
@cestmoi5248
@cestmoi5248 2 жыл бұрын
wow, very well explained. Thank you
@jeffreydwightryanriley8308
@jeffreydwightryanriley8308 2 жыл бұрын
Is variable type even necessary? That’s why I like php. The types can be manipulated in the database and queries are objects. It allows synthetic interaction essentially in that the programmer ultimately codes states versus implicit phases of data. Such as arrays of arrays.
@bextrekks8354
@bextrekks8354 2 жыл бұрын
lmao, that head pop up at the beginning cracks me up!
@altamashsabri8142
@altamashsabri8142 2 жыл бұрын
Amazing explanation, thanks alot
@findlestick
@findlestick 3 жыл бұрын
This is a very useful video.
@cryptoInNutshellR
@cryptoInNutshellR Жыл бұрын
Thanks. Useful, however you still did not cover how to compare 2 objects on the Dog example staring from 05:08
@HIMANSHURAGHAV-km8rr
@HIMANSHURAGHAV-km8rr 3 ай бұрын
Thanks john 😄
@ayanokoji8456
@ayanokoji8456 Жыл бұрын
Wow thanks man it is so well explain !
Array vs. ArrayList in Java Tutorial - What's The Difference?
17:36
Coding with John
Рет қаралды 555 М.
Difference Between "== Operator" And "equals() Method" In Java (Hindi)
15:02
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,2 МЛН
ТВОИ РОДИТЕЛИ И ЧЕЛОВЕК ПАУК 😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН
What type of pedestrian are you?😄 #tiktok #elsarca
00:28
Elsa Arca
Рет қаралды 36 МЛН
Lambda Expressions in Java - Full Simple Tutorial
13:05
Coding with John
Рет қаралды 771 М.
Exception Handling in Java Tutorial
13:20
Coding with John
Рет қаралды 416 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 24 М.
DJI Mic Mini vs Hollyland Lark M2
17:05
Mark Bennett's Camera Crisis
Рет қаралды 1,2 М.
Java Strings are Immutable - Here's What That Actually Means
7:06
Coding with John
Рет қаралды 622 М.
Why I Chose Copywriting as a Beginner
9:08
Harleen Saini
Рет қаралды 296
Germany | Can you solve this? | Math Olympiad Problem
7:16
Master T Maths Class
Рет қаралды 3,6 М.
Vectors in Java: The 1 Situation You Might Want To Use Them
16:13
Coding with John
Рет қаралды 85 М.
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,2 МЛН