• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

difference between equals, contentequals in Strings?

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both are used to check whether two strings are equal or not...Please explain i cant able to figure it out..
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The contentEquals() methods are convenience methods for comparing the character sequence that the String represents to the character sequence that a StringBuffer or CharSequence implementation represents.
The equals() implementation of String does conceptually the same thing, but it can only do so for two String instances. Passing anything other than an instance of String to the equals() method would yield false.
The rules established in the general equals() contract would make it impossible to implement equals() any other way, and furthermore the equals() implementation of String predates the existence of the CharSequence interface and some of its implementations.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

siva chaitanya wrote:Both are used to check whether two strings are equal or not...Please explain i cant able to figure it out..


equals() can only compare two Strings; contentEquals() takes a CharSequence, and so can be used to compare a String with any CharSequence object (which also includes another String). From what I can see, if contentEquals() IS called with another String, it will produce exactly the same result as equals() (although it probably won't be quite as fast).

Winston

[Edit] Dang! Too slow.
 
Ranch Hand
Posts: 231
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you see the prototypes:

public boolean equals(Object obj), and public boolean contentEquals(CharSequence cs).

Think simply, you can use equals method to compare any Object, but contentEquals method compares only strings here...
In java.lang.String class, the toString() method is overridden, so it returns the value in the object, i.e., some String, and not String@12avbe (some hash code), so using both these methods for String would yield same results in my sense, but while you are comparing for other classes which do not implement toString() method in a recommended way (return the contents of the object), differences may be pronounced loud....

You have to read the API for understanding: java.lang.String

Regards
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajdeep Biswas wrote:If you see the prototypes:
public boolean equals(Object obj), and public boolean contentEquals(CharSequence cs).
Think simply, you can use equals method to compare any Object, but contentEquals method compares only strings here...


You're right in principle, but the fact is that a properly written equals() method generally only allows comparisons between like objects (ie, objects in the same hierarchy); and since String is final, the only "like" type is another String.

As for all that stuff about toString(): it has no bearing on the case whatsoever. What Siva needs to do is read the documentation for String.equals() and String.contentEquals() because it explains exactly what the comparisons do.

Winston
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you guys explain me with a example i gone through the documentation but it provides only definition
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you really not work it out?
 
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic