Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

efficiency of import statements  RSS feed

 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when using an import statement, does the compiler include every class within a package, or only the classes that are used?
for example, if a program had "import java.io.*" but then only used a File object, would the compiler throw in BufferedReader and all the other classes in to the compiled code? i've heard both sides of the story, but haven't found official documentation on it.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The import statements have no affect what so ever of performance.
You can include as many as you like and it wont affect performance at all (same performance as not including any).
The only thing an import statement does if to make it easier for us programmers to TYPE.
When we use import we dont have to type the full name of teh class (Package and all) - instead we use import and we can only type the class.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java unlike C++ classes are loaded as needed, not all at the beginning of the program. Further, in Java class definitions will be unloaded if they are not currently being used and memory gets tight. This is an advantage over c++ who can not nativly unload class definitions at runtime.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!