I'm from a C++ background and just started Java today. Say I have a class with a couple of data members. For example:
public class Person {
//Constructors/Destructor
public Person(String strFirstName, String strLastName) {
m_strFirstName = strFirstName;
m_strLastName = strLastName;
m_strFullName = m_strFirstName + m_strLastName;
}
//Getters
public String GetFullName() { return m_strFullName; }
public String GetFirstName() { return m_strFirstName; }
public String GetLastName() { return m_strLastName; }
//Private Data Members
private String m_strFirstName;
private String m_strLastName;
private String m_strFullName;
}
Now lets say I do this:
Person john = new Person("john", "doe");
Person johndoe = new Person("john", "doe");
if (john == johndoe) {
System.out.println("They are Equal");
} else {
System.out.println("They are NOT Equal");
}
Here the result is "They are NOT Equal". I understand this is because Java is comparing the references (memory addresses), and since they are different locations in memory the test fails. I have read that Java doesn't support operator overloading, so I can't overload the operator==, so is there a method I'd override to implement my memberwise comparison? The object.equals method looked promising, but I've read that it's bad practice ot override this one.
UPDATE: Ok I'm convinced overriding equals is OK! I can't find that article that said that it's bad. Thanks for the help, I'll probably have more questions as I learn Java!!