• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals operator

 
Sophie Angela
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class first{
public static void main(String[] args){
first p=new first();
first q=new first();
if(p.equals(q))
System.out.println("Equals");
else
System.out.println("Not Equals");
}
}

as per this code...it prints not equals..because p and q are 2 different objects..but the same checking can be done with p==q..so what is the difference between equals method and == operator......
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jaiganesh Elumalai:
public class first{
public static void main(String[] args){
first p=new first();
first q=new first();
if(p.equals(q))
System.out.println("Equals");
else
System.out.println("Not Equals");
}
}

as per this code...it prints not equals..because p and q are 2 different objects..but the same checking can be done with p==q..so what is the difference between equals method and == operator......


check this link
Working of equals() Operator.
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can override equals method to provide a different logic
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic