This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String's compareTo() query

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While doing comparison via Comparable interface in compareTo () method when we call

this.value.comapreTo(obj.value);

In case if value is String it will call String's compareTO() method, but what if value is other than String? Does it make sure that value should always be String or is there any other way to do it?
 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparable was generified; Comparable<String> implies compareTo(String)

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if the object i pass doesn't contain any string and class of that object is not implementing Comparable interface?
 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it is a somewhat funny situation. Albeit the JDK doucmentation lists this compareTo method:


There seems to exist an overloaded compareTo(java.lang.Object) method as well - maybe a remnant from the good old pre-generic days:


Now this code compiles with 1.4 but not with the newer versions:


However if run with 1.4 or newer versions (1.5, 1.6 or 1.7) we obtain this:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at java.lang.String.compareTo(String.java:108)
at aa.main(aa.java:12)

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaustubh G Sharma wrote:what if the object i pass doesn't contain any string and class of that object is not implementing Comparable interface?


Then the statement won't type-check and the compiler won't allow it. The compiler only allows method calls it knows are valid.
 
Tony Docherty
Bartender
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:Well, it is a somewhat funny situation. Albeit the JDK doucmentation lists this compareTo method:


That's because it implements Comparable<String>

Ivan Jozsef Balazs wrote:There seems to exist an overloaded compareTo(java.lang.Object) method as well - maybe a remnant from the good old pre-generic days:

This is down to the way generics are implemented using type erasure. In reality at the p code level there is no compareTo(String anotherString) method it is in fact compareTo(Object anotherString), but the compiler knows that the generic declaration states the parameter must be of type String and so enforces it at compile time. At run time there is no way of checking what type it should be restricted to.

Ivan Jozsef Balazs wrote:
Now this code compiles with 1.4 but not with the newer versions:
...

That's because prior to Java 1.5 there were no generics and the compareTo method takes an Object as the parameter. From 1.5 onwards the String class was changed to implement the generified Comparable<String> interface and so now the compiler knows that only objects of type String can be passed in to the String classes compareTo() method.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
means in the below sorting code, Integer object is pass as a String object or Object's object... then it is sorted?? but on what basis is sort it. what would be the sorting technic?




>
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what in case of comparator, String doesn't provide any implementation for Comparator interface, then calling comapraTo method how perform sorting. Does Comparator extends Coparable???
 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now id is of the type Integer, and Integer has this method:



What is the problem?
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GOT it..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic