File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string declaration

 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what is the difference in s2 and s3 declaration?
here hashcode value of s1,s2,s3 is same.
but s1==s2 returns true and
s1==s3 returns false
does hashcode value play any role in the object identity check?
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gajendra Kangokar wrote:does hashcode value play any role in the object identity check?


No. It shouldn't even play a role in an equals() check, although it should be consistent with that.

As you've seen, s3 forces the creation of a new String, whereas s2 allows the reuse of an existing String as an optimisation. The other important difference is that there's never any reason to use the s3 style.
 
Tim Cooke
Sheriff
Pie
Posts: 2706
110
Clojure IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was a pretty detailed discussion on the topic of String comparisons last week
http://www.coderanch.com/t/614460/java/java/string-comparision
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the actual use of hashcode..?
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gajendra Kangokar wrote:what is the actual use of hashcode..?

The documentation of hashCode method in Object class says:

This method is supported for the benefit of hash tables such as those provided by {@link java.util.HashMap}


which indicates that the hashMap is used in whenever some object is used as a key in the Map.
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if two objects have equal hasCode doesn't indicate that the two objects are equal. One can always override hasCode() method to return constant value for any object of a given class something like:
 
drac yang
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in String's overridden hashCode and equals method, actually i didn't find direct relation between hashCode and equals, they seem to be independent with each other.



but they two do have relations in String class, that is the content of the string, i.e. the internal char array. only if every chars are same, they would have same hashCode and true equality.

and generally, for Object, only if the two objects have same hashCode, could they equal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic