• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding CompareTo in java

 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Added code tags. See UseCodeTags for details]


The above compareTo method giving error that it should return integer ,can anyone tell please tell why.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your compareTo method return if name.compareTo(c.name) is not zero?
 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew ,i got it we need to return something if the IF condition is not satisfied
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So where you put return statement if the IF condition is not satisfied and what value you will return?
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thakur Sachin Singh wrote:So where you put return statement if the IF condition is not satisfied and what value you will return?


What is returned is specific to the OP class -- I am not sure if the OP mentioned it in detail in this topic. And how it is done would depend on how complex it is -- and again, specific to the OP class. The Java compiler simply ensures that all code paths through the method returns a value.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic