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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody how's it going in this video we're going to talk all about double equals versus dot equals in java by the end of this video you're going to have a deep understanding of the difference between double equals and dot equals and know exactly when to use each if this is your first time on this channel i'm john lee java software engineer and i make java tutorial videos just like this one every single week so be sure to hit that subscribe button so you get the new tutorial every week so when you first learned java after you messed with just hello world you probably made some ants right you had int and one equals three you know and and two equals four or maybe you got these ends as input from the user or something and then maybe you wrote something to see if they were equal to each other so you probably did something like if and one is equal to int two then you know what print out the numbers are equal else you print out numbers are not equal and you can run that and of course 3 should not equal 4 so the numbers are not equal and of course if they are equal we set them both to 4 we can run it again and we get that the numbers are equal and that absolutely does work perfectly for inst that is what you should be doing but what happens if you do that with a different kind of variable in java so instead of an int you do something that's not a primitive type but it's still a pretty simple type in java a string so let's say instead of ins you have string string one equals new string hello and then you also have string string two equals new string and also give it the exact same value hello so those strings should be identical right but if we paste these values in here and do the exact same comparison we can say strings instead we run that we get the strings are not equal now why is that and we could even like print out both of those strings and prove that they have exactly the same values right string one and string two so we can see they have exactly the same values and yet java thinks they're not equal why is that the whole reason for that all boils down to what java is actually doing when you're doing this double equals check what is actually going on is java is checking the underlying memory location of those two objects so i know that all sounds really technical but it's really not that scary let's talk about what happens when you actually call like new string here what java is doing is creating a brand new object at a new memory location and then creating this variable string one that points to that memory location and the same thing here for the second string it's creating another string object at a different memory location and having creating this variable string two that points to that second location and so what this double equals is actually doing is comparing the memory locations of those two objects and of course they're different so you get false so you might be asking why did it work fine for our int variables when our numbers were the same java correctly said that yes those were equal well in the case of a primitive like an int or a float or a double or a short or a long or any of those other java primitive types primitive variables are referring to their actual values and not the memory locations where an object is stored so when you have something like int and one equals one and and two equals two now in java primitive variables represent that value itself and not a reference to it but for everything that isn't a primitive all variables of every other type are a reference to a memory location that has the object so in short when do you use double equals it's when you are comparing two primitive types boolean byte short int long char float and double so if you are doing anything with those eight primitive types in java feel free to use the double equals and everything will work out great so what about every other object type in the world what do you do when you want to compare two objects that aren't primitive to see if they're equal well you use the dot equals method on your objects and you do that like this it's offered in the string class um so you call string1 dot equals and you pass in the other string that you want to check or whether it's equal to and so now we can go ahead and run this code again and see what we see we see the strings are equal and it's working great so now the reason that works is because this equals method has been defined on the string class and if we want to like really nerd out we can like control click and eclipse into this equals method and string and see what it's actually doing to see if these strings are equal but you can see it gets real technical real quick but basically it's going to go through both of those strings and make sure every character matches with every other character in the string and return true if that's the case and if not it'll return false basically for every other type of object you use in java it should offer a dot equals method so you can see if one object of that class equals another object of that class and that'll be the case for just about like every library you use like of course strings collections everything offered in the main java libraries will have implemented an equals method for you to use and you can depend on them so what if you're creating your own classes like i've got a dog class here where i just have an age and a name on a dog and gutters and ciders for that right now i can go back into my program and create two dogs right so i can say dog my dog equals new dog and dog your dog equals new dog and i can try and compare them using dot equals and we'll change this to dogs now you might be thinking now john that should work right you said for everything else but primitives you should use dot equals so let's go ahead and run that and what do you think we're going to get well it says the dogs are not equal so john you said we've got two for primitives we use the double equals and for everything else we use the dot equals and everything will be all right but what went wrong there we have two brand new dogs there couldn't be anything possibly different about them so why aren't they equal and the reason is because this dot equals method hasn't been implemented yet in my dog class and so what happens is if we don't implement the dot equals method in our own classes when we create them it uses the default implementation of the dot equals method from the object class in java the parent of all other classes in java is the object class and it defines a dot equals method so if we don't define one that's the one it uses and guess what you think it does let's actually dive into it and see that's right it just returns the value of the double equals check on the object anyway so the lesson here if you don't actually implement the dot equals method in your class it's just as bad as using the double equals so how do you go about implementing the equals method in your classes all you need to do is go in and actually write that method in your class so you just have to have a public boolean equals is the name of the method boolean is the return type it returns true or false for whether it equals uh the other object being passed in and object is the type that needs to be passed in it's usually called and you write your own implementation of what it means for one dog to be equal to another dog and you return true if they're equal and false if they're not there's a little bit more complexity around that involved like if something other than a dog gets passed in or if it's null or then weird things like that that'll be the subject of another video writing a good equals method but that's basically how you go about doing it now here's some super strange bit of java knowledge that you can use to impress all the nerds out there so let's go ahead and change this back to a double equals right string one and string two and of course as we know java says they're not equal because they're two separate objects and they have separate memory locations but if we change these string declarations into instead of using a new string we just use what's called a string literal and we set it like this which is probably how you're used to setting strings and run it again we get the strings are equal what well that has to do with the strange way that java actually holds these strings behind the scenes when you declare them like this if you have two different variables that refer to the same string literal java will just hold that string literal in one place in memory and have both of those variables refer to that one place if you don't use the new string command so java is doing what is called interning those strings it's kind of a fancy thing it does behind the scenes in order to save memory so if you have a whole bunch of string literal variables that refer to the same string java is going to only condense those down into one memory location to save on memory when it can but if you use new like we had before java won't do that so that's like weird right like sometimes for strings this double equals works and sometimes it doesn't lesson learn though right just don't use it for anything except for your primitive data types that we went over before strings dogs any other type of object in the world that you want to see if two of those objects are equal always use that object's dot equals method for primitives use double equals and you'll be good to go if you learned a little something in this video let me know with a like if you'd like to see more like this every week be sure to hit the subscribe button and thanks for watching i'll see you guys in the next video
Info
Channel: Coding with John
Views: 3,573
Rating: undefined out of 5
Keywords: java equals vs ==, java equals method, java == vs .equals, == in java, == and .equals difference in java, == and equals in java, java string comparison, java string comparison not working, java string comparison returns false, java equals check, java equals tutorial, java program, java, java programming, codingwithjohn, coding with john, java ==
Id: AoUVdLWLFQw
Channel Id: undefined
Length: 8min 48sec (528 seconds)
Published: Sat Jan 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.