why below program resulting false

package test;

public class CustomerTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
Customer c1=new Customer("john","k",1);
Customer c2=new Customer("mike","e",2);

System.out.println("c1 equals to c2..content check --->"+c1.equals(c2));
System.out.println("c1 == to c2 referene check--->"+ (c1==c2));


System.out.println("c1 equals to new object..content check --->"+c1.equals(new Customer("john","k",1)));// i wonder why this is false
System.out.println("c1 == to new object referene check--->"+(c1 == new Customer("john","k",1)));


	}

}

Open in new window



package test;
public class Customer {
    String firstName;
    String lastName;
    int ssn;
    public Customer(String firstName, String lastName, int ssn) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.ssn = ssn;
    }
    public String getFirstName() {
        return this.firstName;
    }
    public String getLastName() {
 
        return this.lastName;
    }
    public int getSsn() {
        return this.ssn;
    }
}

Open in new window


i ran above code and got below output

c1 equals to c2..content check --->false
c1 == to c2 referene check--->false
c1 equals to new object..content check --->false
c1 == to new object referene check--->false


I thought below is true

System.out.println("c1 equals to new object..content check --->"+c1.equals(new Customer("john","k",1)));// i wonder why this is false

i see content is same so i thought it result true.

please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
the "object".equals function only returns true if really it IS the same object, not if all the properties have the same values.

to get "true" while comparing "all" of the properties you need to implement a comparison function method in the Customer class, overriding compare function:
https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
CEHJCommented:
Equality is a separate issue from natural ordering and therefore Comparator has nothing to do with it.
gudii - i would have thought after all these questions, you would know by now that you need to override equals to have proper object equivalence checking

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
yea. you are right. Same issue of not overriing equals and hashCode methods

package test;

import java.util.ArrayList;
import java.util.List;

public class CustomerTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Customer c1 = new Customer("john", "k", 1);
		Customer c2 = new Customer("mike", "e", 2);

		System.out.println("c1 equals to c2..content check --->"
				+ c1.equals(c2));
		System.out.println("c1 == to c2 referene check--->" + (c1 == c2));

		System.out.println("c1 equals to new object..content check --->"
				+ c1.equals(new Customer("john", "k", 1)));// i wonder why this
															// is false
		System.out.println("c1 == to new object referene check--->"
				+ (c1 == new Customer("john", "k", 1)));
		
		List list=new ArrayList();
		list.add(c1);
		list.add(c2);
		
		if(list.contains(new Customer("john", "k", 1))){
			System.out.println("yes list contains john k 1");
		}else {
			System.out.println("no list does not contains john k 1");
		}
		

	}

}





package test;
public class Customer {
    String firstName;
    String lastName;
    int ssn;
    public Customer(String firstName, String lastName, int ssn) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.ssn = ssn;
    }
    public String getFirstName() {
        return this.firstName;
    }
    public String getLastName() {
 
        return this.lastName;
    }
    public int getSsn() {
        return this.ssn;
    }
}




c1 equals to c2..content check --->false
c1 == to c2 referene check--->false
c1 equals to new object..content check --->false
c1 == to new object referene check--->false
no list does not contains john k 1




when i override the equals and hashcode

package test;

import java.util.ArrayList;
import java.util.List;

public class CustomerTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Customer c1 = new Customer("john", "k", 1);
		Customer c2 = new Customer("mike", "e", 2);

		System.out.println("c1 equals to c2..content check --->"
				+ c1.equals(c2));
		System.out.println("c1 == to c2 referene check--->" + (c1 == c2));

		System.out.println("c1 equals to new object..content check --->"
				+ c1.equals(new Customer("john", "k", 1)));// i wonder why this
															// is false
		System.out.println("c1 == to new object referene check--->"
				+ (c1 == new Customer("john", "k", 1)));
		
		List list=new ArrayList();
		list.add(c1);
		list.add(c2);
		
		if(list.contains(new Customer("john", "k", 1))){
			System.out.println("yes list contains john k 1");
		}else {
			System.out.println("no list does not contains john k 1");
		}
		

	}

}






package test;
public class Customer {
    String firstName;
    String lastName;
    int ssn;
    public Customer(String firstName, String lastName, int ssn) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.ssn = ssn;
    }
    public String getFirstName() {
        return this.firstName;
    }
    public String getLastName() {
 
        return this.lastName;
    }
    @Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result
				+ ((lastName == null) ? 0 : lastName.hashCode());
		result = prime * result + ssn;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Customer other = (Customer) obj;
		if (firstName == null) {
			if (other.firstName != null)
				return false;
		} else if (!firstName.equals(other.firstName))
			return false;
		if (lastName == null) {
			if (other.lastName != null)
				return false;
		} else if (!lastName.equals(other.lastName))
			return false;
		if (ssn != other.ssn)
			return false;
		return true;
	}
	public int getSsn() {
        return this.ssn;
    }
}




c1 equals to c2..content check --->false
c1 == to c2 referene check--->false
c1 equals to new object..content check --->true
c1 == to new object referene check--->false
yes list contains john k 1

Open in new window


now after overriding both methods i am getting desired output. thank you
CEHJCommented:
:)

SSNs are unique. That's all you need as the discriminator  for hashCode and equals
gudii9Author Commented:
sure
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.