aspose file tools*
The moose likes Java in General and the fly likes String's compareTo() query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Watch "String New topic
Author

String's compareTo() query

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

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?


No Kaustubh No Fun, Know Kaustubh Know Fun..
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 871
    
    5
Comparable was generified; Comparable<String> implies compareTo(String)

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

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

Joined: May 22, 2012
Posts: 871
    
    5
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

Joined: Apr 06, 2010
Posts: 4457
    
    8

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

Joined: Aug 07, 2007
Posts: 2359
    
  50
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

Joined: May 13, 2010
Posts: 1281

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

Joined: May 13, 2010
Posts: 1281

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

Joined: May 22, 2012
Posts: 871
    
    5


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



What is the problem?
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

GOT it..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String's compareTo() query