toString and equals are both child of Object Class.
toString method should be override in the class to acieve meaningful output.
@Override
public String toString(){
return ; //return String
}
== operator works properly only on primitive type. eqauls method works for comparing Objects for most part.
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instance ClassName)) {
return false;
}
// custom logic here
}
Workshop 8
@Override
public String toString(){
return String.format("%-10s%-15s%-10d%-10.2f%-15s",
this.firstName, this.lastName, this.grade, this.department);
}
@Override
public boolean equals(Object o){
if(o == this) return true;
if(o == null ) return false;
if(!(o instanceof Student)) return false;
Student student = (Student) o;
return student.firstName.equals(firstName) && student.lastName.equals(lastName) &&
student.grade == grade && student.department.equals(department);
}