• Post Reply Bookmark Topic Watch Topic
  • New Topic

import  RSS feed

 
Anil Kumar Saha
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody please help me to clear this doubt?

suppose I need only to import Vector class,so what is the difference
between this two imports from performance point of view?

(1)import java.util.*;
(2)import java.util.Vector;
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually either version compiles to exactly the same bytecode, so no performance difference. The compiler determines the fully-qualified name of every class your code references and embeds the names into the classfile. Import statements are just a little syntactic sugar to save you the trouble of repeatedly typing the complete name java.util.Vector everywhere your code needs to refer to the Vector type.
 
John Dunn
slicker
Ranch Hand
Posts: 1108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I once went to one of those java training camps, (for JINI), and the example station that was setup wouldn't work. A few SUN programmers were banging away it for awhile until someone decided to make all the import explicit. Turns out there was the same named class in two packages that never got picked up. Yikes. Well it probably won't happen often, but when it does it'll be a real pain. I ALWAYS use explicit imports.

Also, I like to look in a class and know which classes it uses. So if you are working in some new class and notice that it only access two other classes, you'll quickly get an idea of what it probably does or does not do. Alos, very helpful to get new programmers up to speed, and when you go looking through very old code. (in my opinion...)
 
Michael Duffy
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Weitzman:
Actually either version compiles to exactly the same bytecode, so no performance difference. The compiler determines the fully-qualified name of every class your code references and embeds the names into the classfile. Import statements are just a little syntactic sugar to save you the trouble of repeatedly typing the complete name java.util.Vector everywhere your code needs to refer to the Vector type.


Exactly. Don't confuse "import" with "class loading".

And follow the advice to always spell out all your imports. It's better documentation that using the "*" notation. Make clear what you're using.

It's especially easy if you have an IDE like IntelliJ that helps you manage imports. But I still used to spell them out, even before I got my hands on IntelliJ.
 
Anil Kumar Saha
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks a lot for clearing my doubt.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!