aspose file tools*
The moose likes Java in General and the fly likes Working of the toString( ) method and compareTo( ) method and comparator in given code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Working of the toString( ) method and compareTo( ) method and comparator in given code" Watch "Working of the toString( ) method and compareTo( ) method and comparator in given code" New topic
Author

Working of the toString( ) method and compareTo( ) method and comparator in given code

Rachit Kumar Saxena
Ranch Hand

Joined: Dec 24, 2011
Posts: 50
Hello everyone!!! I typed the following code:


Why do we need to override toString( ) method.Please descibe in detail its behaviour in above detail.Also I seek your valuable opinions about:
1.When is the compareTo( ) method invoked?
2.How does the Collections.sort( ) function in above code i.e. how does it fuction.Can you suggest how do we make a code like that in java without using libraries.Plese reply soon!!!


[HENRY: added code tags]
[Rob: improved indentation]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Please go back to your post and add code tags, so we can actually read your code.
I think you will have to work out what those methods do, which you can best do by finding the 3rd chapter of Joshua Bloch’s Effective Java™. You might find a reference to it is you search this forum; there will be links to the old edition because I posted them. Also look up the java.util.Comparator interface.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hello Rachit Kumar Saxena,

Welcome to CodeRanch!

Please UseCodeTags. It becomes difficult to read non-indented and non-formatted code.

Also, try to use meaningful names instead of t and t1 - say title and artist.

Now, coming to your questions:
Rachit Kumar Saxena wrote:Why do we need to override toString( ) method

You don't. toString method is used when you are printing the object directly. If you are not doing it, there's no need to override toString.
Rachit Kumar Saxena wrote:Please descibe in detail its behaviour in above detail

You can get it in javadoc of Object class.
Rachit Kumar Saxena wrote:1.When is the compareTo( ) method invoked?

During Collections.sort(s)
Rachit Kumar Saxena wrote:2.How does the Collections.sort( ) function in above code i.e. how does it fuction

As said, it will call compareTo method, which will decide the logic behind smaller and larger object. E.g. currently, a song(again, why is it not 'Song' with capital S?) will be less than another song if first song's title is alphabetically less than other song's title.
Rachit Kumar Saxena wrote:Can you suggest how do we make a code like that in java without using libraries

Well, why do you want to do that? You are not using any third party libraries. Anyways, it can be done by comparing the song objects from the ArrayList to each other manually (i.e. you'll have to actually invoke the method) and the sorting also would have to be done manually (e.g. using algorithms like bubble sort etc.)

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Campbell Ritchie wrote:Please go back to your post and add code tags, so we can actually read your code.



Okay, I added the code tags -- and I have to say that this is probably the most ugliest formatted code that I have even seen..... regardless, I guess it is still better than unformatted code.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

I don't see any problems with the formatting


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Working of the toString( ) method and compareTo( ) method and comparator in given code