Comparing User Defined class with equals and ==

First lets compare without overridingg equals method

  
package Equalsand_Twoequals;

public class StudentCompare {

	public StudentCompare(String studentRollNo, String studentName) {
		// TODO Auto-generated constructor stub
		this.studentName = studentName;
		this.studentRollNo  = studentRollNo;
	}
	private String studentRollNo;
	private String studentName;
	public String getStudentRollNo() {
		return studentRollNo;
	}
	public void setStudentRollNo(String studentRollNo) {
		this.studentRollNo = studentRollNo;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	
	public static void main(String[] args) {
		StudentCompare compare1 = new StudentCompare("001","Sridhar");
		StudentCompare compare2 = new StudentCompare("002","Pathy");
		StudentCompare compare3 = new StudentCompare("003","Pathy");
		StudentCompare compare4 = compare3;
		
		System.out.println("Equals");
		System.out.println("-----");
		System.out.println(compare1.equals(compare2)); // false
		System.out.println(compare1.equals(compare3)); // false
		System.out.println(compare2.equals(compare3)); // false
		System.out.println(compare3.equals(compare4)); // true // since it is same memory location 
		System.out.println(compare2.equals(compare4)); // false
		
		System.out.println("==");
		System.out.println("--");
		System.out.println(compare1==compare2); // false
		System.out.println(compare1==compare3); // false
		System.out.println(compare2==compare3); // false
		System.out.println(compare3==compare4); // true
		System.out.println(compare2==compare4); // false
	}
}

Output will be
Equals
—–
false
false
false
true
false
==

false
false
false
true
false

package Equalsand_Twoequals;

public class StudentCompare {

	public StudentCompare(String studentRollNo, String studentName) {
		// TODO Auto-generated constructor stub
		this.studentName = studentName;
		this.studentRollNo  = studentRollNo;
	}
	private String studentRollNo;
	private String studentName;
	public String getStudentRollNo() {
		return studentRollNo;
	}
	public void setStudentRollNo(String studentRollNo) {
		this.studentRollNo = studentRollNo;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		StudentCompare compare = (StudentCompare)obj;
		if(compare.studentName.equalsIgnoreCase(this.studentName)){
			return true;
		}
		return false;
	}
	public static void main(String[] args) {
		StudentCompare compare1 = new StudentCompare("001","Sridhar");
		StudentCompare compare2 = new StudentCompare("002","Pathy");
		StudentCompare compare3 = new StudentCompare("003","Pathy");
		StudentCompare compare4 = compare3;
		
		System.out.println("Equals");
		System.out.println("-----");
		System.out.println(compare1.equals(compare2)); // false
		System.out.println(compare1.equals(compare3)); // false
		System.out.println(compare2.equals(compare3)); // true
		System.out.println(compare3.equals(compare4)); // true // since it is same memory location 
		System.out.println(compare2.equals(compare4)); // true
		
		System.out.println("==");
		System.out.println("--");
		System.out.println(compare1==compare2); // false
		System.out.println(compare1==compare3); // false
		System.out.println(compare2==compare3); // false
		System.out.println(compare3==compare4); // true
		System.out.println(compare2==compare4); // false
	}
}

Output will be
Equals
—–
false
false
true
true
true
==

false
false
false
true
false

One thought on “Comparing User Defined class with equals and ==

Leave a comment

Your email address will not be published. Required fields are marked *