• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Compiler error vs runtime error

 
Ranch Hand
Posts: 47
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would like to know why does not the compiler report error about a TreeSet without Comparable implementation on user-defined classes. Why does it have to be a runtime error?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepa Sreedhara wrote:
I would like to know why does not the compiler report error about a TreeSet without Comparable implementation on user-defined classes. Why does it have to be a runtime error?



As you know, it is possible to create, and then use a TreeSet that works on objects that are not Comparable. How would the compiler be able to tell how a TreeSet instance (which doesn't exist at compile time) was created so it can confirm whether non-Comparable objects are allowed?

Henry
 
Deepa Sreedhara
Ranch Hand
Posts: 47
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not understand the first sentence much, "As you know, it is possible to create, and then use a TreeSet that works on objects that are not Comparable."

The second sentence clarified my doubt though. Thank you.
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepa Sreedhara wrote:I did not understand the first sentence much, "As you know, it is possible to create, and then use a TreeSet that works on objects that are not Comparable."



You can configure a TreeSet to work with non-Comparable instances, meaning instances that don't support the Comparable interface, and it can work fine. It can work correctly, without any Runtime exceptions, assuming that you provided a Comparator when creating the TreeSet.

Henry
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic