Java Tutorial - Compare Objects - Equality Operator Vs Equals Method

preview_player
Показать описание
Compare Objects - Equality Operator Vs Equals Method.

Hi Everyone and Welcome,

I am continuing on with my video series on Java programming for beginners. In this video, I explain and illustrate using examples a common problem encountered by almost all beginner programmers - the difference between the equality operator '==' and the equals method 'equals()' when comparing Java objects.

I hope you enjoy this video. Any and all comments or feedback is welcome. Thank you.

WATCH SOME MORE JAVA TUTORIALS

Enhanced For Loop Explained and Illustrated

For Loops Explained and Common Pitfalls Highlighted

How to use the Shortcut Operators

How to use the Conditional Operator ( ? : )

Passing by Value (Object References)

Command Line Arguments

How To Write A Method Part II

How To Write A Method Part I

Passing by Value (Primitive Data Types)

Division and Modulo Operators Explained

Operator Precedence Explained

What are Data Types, Variables and Constants?

How to use Nested If Statements

How to use simple If Statements

What are Control Flow Statements

How to Write, compile and run your first Java program on Windows

How to write, compile and run Java programs all within Notepad++

How to download and install the Notepad++ editor on Windows
Рекомендации по теме
Комментарии
Автор

Helpful video .. Thank you
It would be great if some examples included (so we can see the difference in the output)

someone
Автор

This haunted me for a while, I feel a weight off my shoulders! Thank you Martin!!!

KevinBFG
Автор

Best explanation I've seen. And trust me, I've watched a few videos. Your work is appreciated!

selbywebb
Автор

Awesome teaching!!! Im totally new in Java field and I love all your videos hope continue to educate us. Please make videos for "Generics and Collections" ...Thank you very very much it helps a lot :)

katy
Автор

Thank you Martin you did GREAT professional explanation

ca
Автор

Good Man Martin, Great Video ! very well described & clear

AndrewOnTestingCars
Автор

Awesome explanation! Thank you very much! n.n

makeupyonkis
Автор

Awesome Explanation, but should have shown the output.

callmeMsMori
Автор

thx i was going through asserts for Junit and had a hard time understanding the difference between assertEquals and assertSame. From this video seems like assertSame is using == and assertEquals is using .equals(). If the class doesn't have it's own equal method it will act the same way as assertSame or the class it extends that does have a equals method.

seal
Автор

Hey Martin, Nice video, appreciate it. I have a question, What if I have 100 fields in my class, will i have to write the code to compare all the fields in the overridden equals method

jimcherian
Автор

Respected Sir, good explanation. I am new in CSC field. I have written a program in which I have to compare two object's variables. Instance variables are private. 1 object is derived by set and get methods and other object information is derived by prompting the user to get variables for other object to be compared. How to compare in this one with equal method.
Yours Sincerely.

mayankpatel
Автор

great video, explained very well! thanks!

PapaSmurff
Автор

Great video Martin. Shouldn't you always override the hascode() method as well?

Lanceegan
Автор

great video, very clear, thanks a lot !!

Crisikos
Автор

Great tutorials. Too bad you have only 19.

ivanpantic
Автор

Not impressed, because of a few very basic, yet major issues with your example:
1. The code you show as example will never compile: is missing a closing )
2. A wee bit more important: in your equals method, you compare the values of String (Objects) by using a referential (==) comparison. Your entire point is to teach people not to compare Objects like this.
3. Most important of all: do not, seriously, not, overload the equals method. You should override it. It should always keep the signature:

public boolean equals(Object o) {...}

don't let it take an instance of your class. If you do, you 'll be in trouble once you start working with sorting of collections and such.

hansbrinkhof