• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Import Question

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone provide an explanation into exactly what happens when you use the import statement? Specifically, do you suffer a performance hit at runtime by doing something like



as opposed to

?

When do classes actually get loaded--when they're first imported, when they're first used, or at some other time? Thanks.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will only import the necessary classes/modules used in your program. So no, there is not a performance hit. But, for code clarity I was taught (and recommend) to use the specific import. If you are using Eclipse, this is very easy to do. Just let Eclipse import it for you by clicking on the error x and selecting the proper import statement. Not sure about NetBeans, but I imagine it works the same way.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, in NetBeans also it works the same way.
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Brad said, it has no effect on performance at all. This is because imports are not a runtime mechanism - they are only used for the compiler to know where to find each class. You can even omit imports altogether, but that would require you to write the full class name (including package) every time.

In byte code, there is never a reference to File, for instance. Instead, there is a reference to java.io.File - as if there never was any import.
 
Marshal
Posts: 79464
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better way to do it in Eclipse: write "arr" then ctrl-space. YOu get a drop-down list, find ArrayList (java.util) on that drop-down list, click it, and Eclipse will do all the importing automatically.

In NetBeans the easiest thing to do is write "java" then you get a drop-down list: click java, then on the next list click util then on the next list click ArrayList.

Or something very similar.
 
J. Ryan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, that all makes sense. That was my initial guess, but I just wanted confirmation. Thanks for all the replies.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dont imports work in a simular way to referances in the sense that they just point to the API?
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.Date;
import java.io.*;

The star form increase the compilation time- especially if you import several large packages. For this reason it is a good idea to explicitly name the classes that you want to use rather than importing the whole packages.However the star form has absolutely no effect on the run-time performance or size of your class.

Reference : The Complete Reference by Herbert Schildt.
 
reply
    Bookmark Topic Watch Topic
  • New Topic