Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Organizing Data Serialization

Pedro Silva
Pedro Silva
5,363 Points

Why do we need to import a Serializable interface package, but not a Comparable?

both are interfaces, but why does one need to be imported as package and the other not?

1 Answer

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi Pedro

The main reason is because Serializable interface is part of a package named java.io meanwhile Comparable interface is part of package called java.lang. Wait! So what is the difference?

Well java.lang package is imported by default to your Java IDE because it includes all object you are basically using in Java coding such as Boolean, String, etc. Feel familiar right with that names? You do not have to import anything to use Boolean and Strings in your code since by default that package is already available to you with your Java IDE. Comparable is part of java.lang package so you do not have to import anything to use Comparable.

However, other package such as java.io is not available by default thus you need to import it. It is for the sake of memory usage efficiency I guess. You do not have to make available things you do not need. If you need it you can simply make it available by using import. Since Serializeable is part of java.io it is not available by default. Since you need one then you must import java.io to make it available to use.

I hope this can help a little.

Pedro Silva
Pedro Silva
5,363 Points

Thank you very much Yanuar!

This part of the course in the Learn Java track throw a lot of new concepts and information at us and sometimes i get lost in what comes from where, but your answer was very helpful.

Again, Thanks!