Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes equals operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals operator" Watch "equals operator" New topic
Author

equals operator

Sophie Angela
Ranch Hand

Joined: Jul 17, 2006
Posts: 46
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

Joined: Dec 27, 2005
Posts: 1249

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.


The Best way to predict your future is to create it - Every great individual common man
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
You can override equals method to provide a different logic


http://www.skillassert.com


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals operator
 
Similar Threads
equals() method
equals and to string method
Exception in thread "main" java.lang.StackOverflowError
Another newbie question
Unexpected behaviour of post-increment : what is happening?