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

Comparing String Values with Equals and ==

package Equalsand_Twoequals;

public class StringCompare {

	public StringCompare() {
		// TODO Auto-generated constructor stub
	}
	public static void main(String[] args) {
		String string1 = "sri";
		String string2 = "sri";
		String string3 = new String("sri");
		String string4 = new String("sri");
		
		System.out.println("EQUALS");
		System.out.println("------");
		System.out.println(string1.equals(string2));
		System.out.println(string1.equals(string3));
		System.out.println(string3.equals(string4));

		System.out.println("==");
		System.out.println("--");
		System.out.println(string1==string2);
		System.out.println(string1==string3);
		System.out.println(string3==string4);
	
		System.out.println("Imp One");
		System.out.println("-------");
		String string5 = new String("sri");
		String string6 = string5;
		System.out.println(string5.equals(string6));  //since string5 and string 6 are equal so true
		System.out.println(string5==string6); //since string5 and string 6 are equal so true // both in same memory location
		//
		System.out.println(string6==string3); //since string3 and string 6 are two different obj so false
		System.out.println(string6==string1); //since string1 and string 6 are two different obj so false
		
		
		String string7 = "sri";
		String string8 = string7;
		System.out.println(string8==string7);
		System.out.println(string8==string1);
		System.out.println(string8==string3);
	}
}

Output EQUALS —— true true true == — true false false Imp One ——- true true false false true true false

Comparing Interger Wrapper class with Equals and ==

    package Equalsand_Twoequals;

    public class IntegerCompare {

	public IntegerCompare() {
		// TODO Auto-generated constructor stub
	}
	public static void main(String[] args) {
		Integer i1 = 10;
		Integer i2 = 10;
		Integer i3 = new Integer(10);
		Integer i4 = new Integer(10);
		int i5 = 10;
		
		Integer i6 = 1000;
		Integer i7 = 1000;
		Integer i8 = new Integer(1000);
		Integer i9 = new Integer(1000);		
		int i10 = 1000;
		/* Equals*/
		System.out.println("EQUALS");
		System.out.println(i1.equals(i2));             
		System.out.println(i1.equals(i3));
		System.out.println(i3.equals(i4));
		System.out.println(i1.equals(i5));
		System.out.println(i3.equals(i5));
		
		System.out.println(i6.equals(i7));
		System.out.println(i6.equals(i8));
		System.out.println(i8.equals(i9));
		System.out.println(i6.equals(i10));
		System.out.println(i8.equals(i10));
		
		
		/*==*/
		System.out.println("==");
		System.out.println(i1==i2);             
		System.out.println(i1==i3);
		System.out.println(i3==i4);
		System.out.println(i1==i5);   // int compared either to Integer or new Integer will result true   
		System.out.println(i3==i5);		 // int compared either to Integer or new Integer will result true	
		
		System.out.println(i6==i7);   // false because == will compare only upto 127 
		System.out.println(i6==i8);
		System.out.println(i8==i9);
		System.out.println(i6==i10); // int compared either to Integer or new Integer will result true
		System.out.println(i8==i10); // int compared either to Integer or new Integer will result true
		
		
		Integer i11 = 10;
		Integer i12 = i11;
		Integer i13 = new Integer(10);
		Integer i14 = i13;
		int i15 = 10;
		
		Integer i16 = 1000;
		Integer i17 = i16;
		Integer i18 = new Integer(1000);
		Integer i19 = i18;		
		int i20 = 1000;
		
		System.out.println("======");
		
		System.out.println(i11==i12);
		System.out.println(i13==i14);   // Imp one
		System.out.println(i14==i11);
		
		System.out.println(i16==i17);	// Imp One
		System.out.println(i18==i19);   // Imp one
		System.out.println(i19==i16);
	}
}
       

Output is

EQUALS
true
true
true
true
true
true
true
true
true
true
==
true
false
false
true
true
false
false
false
true
true
======
true
true
false
true
true
false