Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes methods() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "methods()" Watch "methods()" New topic
Author

methods()

chathuranga
Greenhorn

Joined: Apr 20, 2008
Posts: 4
Whath are the difference between

.equals() method & == operator

I have reffered some topics in API but i have been not find such good results.

would you solve this problem


c.Sampath
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Chathuranga,

When talking about objects, "==" compares references, not values.

"equals()" compares values for equality. Unless equals() is overridden, the default implementation from Object, which merely uses "==".

A useful summary can be found here.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
This is a very important concept to learn and I have covered this along with other fundamentals, with diagrams in a Q&A approach if you want to learn more.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
adi narayana
Greenhorn

Joined: May 08, 2008
Posts: 2
class Test{
public static void main(String args[])
{
String s1="hello";
String s2=new String("hello");
if(s1 == s2)
System.out.println("both are same");
else
System.out.println("not same");
}
}
1) equals() method compare the content of the string
2) == operator compares the reference of the string objects
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Welcome to the Ranch, adi, but you appear to have missed our naming policy onm the way in. Please click "my profile" and correct your displayed name to conform.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: methods()