public class Employee {
private int empno;
private String ename;
private int esal;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getEsal() {
return esal;
}
public void setEsal(int esal) {
this.esal = esal;
}
public Boolean equals(Employee emp){
return this.empno == emp.empno;
}
}
public class EqualsDemo {
public static void main(String[] args) {
Employee emp1 = new Employee();
emp1.setEmpno(100);
emp1.setEname("Narasi");
emp1.setEsal(10000);
Employee emp2 = new Employee();
emp2.setEmpno(100);
emp2.setEname("Narasi");
emp2.setEsal(10000);
String s1=new String("hello");
String s2=new String("hello");
if (emp1.equals(emp2)){
System.out.println("both the object are same emp1:"+emp1.toString()+"\nemp2:"+emp2);
}else{
System.out.println("both the object are not sameemp1:"+emp1.toString()+"\nemp2:"+emp2);
}
if(s1==s2) {
System.out.println("s1==s2 is TRUE");
} else{
System.out.println("s1==s2 is FALSE");
}
if(s1.equals(s2)) {
System.out.println("s1.equals(s2) is TRUE");
} else {
System.out.println("s1.equals(s2) is FALSE");
}
}
}
========================================================================
Output:
both the object are same emp1:
Employee@19821f
emp2:Employee@addbf1
s1==s2 is FALSE
s1.equals(s2) is TRUE
Note: *comparasion of primitives by using == will compare the value.
But comparasion on Object == will compare the memory address.
while comparing the object in the program we have to override the equals method.
if we not override it will compare the two object of the memory location which gives false.
Thanks..........
private int empno;
private String ename;
private int esal;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getEsal() {
return esal;
}
public void setEsal(int esal) {
this.esal = esal;
}
public Boolean equals(Employee emp){
return this.empno == emp.empno;
}
}
public class EqualsDemo {
public static void main(String[] args) {
Employee emp1 = new Employee();
emp1.setEmpno(100);
emp1.setEname("Narasi");
emp1.setEsal(10000);
Employee emp2 = new Employee();
emp2.setEmpno(100);
emp2.setEname("Narasi");
emp2.setEsal(10000);
String s1=new String("hello");
String s2=new String("hello");
if (emp1.equals(emp2)){
System.out.println("both the object are same emp1:"+emp1.toString()+"\nemp2:"+emp2);
}else{
System.out.println("both the object are not sameemp1:"+emp1.toString()+"\nemp2:"+emp2);
}
if(s1==s2) {
System.out.println("s1==s2 is TRUE");
} else{
System.out.println("s1==s2 is FALSE");
}
if(s1.equals(s2)) {
System.out.println("s1.equals(s2) is TRUE");
} else {
System.out.println("s1.equals(s2) is FALSE");
}
}
}
========================================================================
Output:
both the object are same emp1:
Employee@19821f
emp2:Employee@addbf1
s1==s2 is FALSE
s1.equals(s2) is TRUE
Note: *comparasion of primitives by using == will compare the value.
But comparasion on Object == will compare the memory address.
while comparing the object in the program we have to override the equals method.
if we not override it will compare the two object of the memory location which gives false.
Thanks..........
No comments:
Post a Comment