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

Rohit Tolawat
Rohit Tolawat
8,277 Points

The use of java.io.*

I understand the significance of this statement. But I do not understand if there is any effect of this import statement on memory ? For Eg: import java.io.Serializable only imports one class and makes it method (if any) accessible. However, java.io.* exposes all the classes in a given package. Would it require more memory. Is it a good practice to use this format or the one mentioned previously ? Kindly share your thoughts !

4 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Yep, sometimes classes can share the same name, just be in different packages. Therefore, if you import everything you might be pulling across a class with a name that collides with one that you are thinking you are using. It creates really hard to track down bugs.

Most IDE's will expand the import automatically. You're almost there, next course goes over this as well!

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Would it require more memory?

Not at all. This has no impact on your program's runtime performance. Java compiler is optimized in such a way that when your code is compiled into bytecode, it only contains the needed classes from packages that were used in your code, not all the classes from the whole package.

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi Rohit

Mostly it's a matter of style. Some people prefer wildcard import if their code are making using of more than a few classes from the same package, for the sake of simplicity; some folks insist on explicit import, one statement at a time, for better readability of the code. So that's that.

But do keep in mind that, in a special case when you wildcard import 2 packages, and the imported class you're using in the code happen to be found in both packages, it'd run into namespace collision, and cause your code fail to compile.

Since import statement is handled during compilation of the Java code, there's no run-time performance trade-off of the 2 styles of import, in fact, the final bytecode is exactly the same. Personally, I find that explicit import in general makes the overall code easier to read.

Rohit Tolawat
Rohit Tolawat
8,277 Points

Hey William, Thanks for the answer. But then, why do people insist on specifying the class explicitly. Is it a good practice ? Eg: Using import java.util.Set; instead of import java.util.*;