Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between java.util.* and java.util.HashMap

 
Kumara Sharma
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning,

If I just use HashMap in my class, I can always just import java.util.HashMap.
If I am using more classes from a util package, I can say java.util.* / import all the classes individually as above.
I saw code which uses 10 classes from util and the developer had imported all classes individually.

If I import the whole package, all classes in the package are available to my class.
What happens when I import the whole package. Will all the classes are loaded into JVM?
So is it a good idea to import whole package / for optimizing reasons import classes individually.
Which one is better?

Thanks.
 
Stephan van Hulst
Bartender
Posts: 6327
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, nothing will be loaded. It's just a way of telling the compiler where to look for the classes you're using. Some people prefer to import every class individually.

Personally, I use 'mass import' for the well known standard packages. You may expect from any Java programmer worth their salt that they can find a Map in java.util, an InputStream in java.io and a Socket in java.net.

I import:
java.util.*
java.io.*
java.nio.*
java.net.*
java.awt.*
java.awt.event.*
javax.swing.*
javax.swing.event.*

Classes I've written myself, or that are otherwise not found in these packages I mostly import individually.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Importing is a compile time mechanism. It doesn't make any classes being loaded at runtime. Only classes you actually use are loaded. Importing only allows you to write HashMap in your code instead of java.util.HashMap.

As for which one is better: both are. It all depends on your preferences. There are people (and I am one of them) that like to specify each import individually. This has two advantages:
1) it's a bit clearer from which package a used class comes
2) you prevent ambiguities

Let me clarify the second with an example:
If you always specify the imported classes individually (I let Eclipse handle it ) you don't get this problem.
 
Saket Barve
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumara Sharma wrote:

If I import the whole package, all classes in the package are available to my class.
What happens when I import the whole package. Will all the classes are loaded into JVM?


It won't load it'll merely tell the compiler where to look for the class(es). Performance should not really be an issue.


 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I import:
java.util.*
java.awt.*

Classes I've written myself, or that are otherwise not found in these packages I mostly import individually.

And of course java.util.List to get rid of the ambiguity. Which my way of importing prevents.

(Although in small test programs I mass-import as well. Then I'm just being lazy.)
 
Sudipta Laha
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Its better to import individual classes.
The reason is that if there is a class with same name in two package, importing individual classes will help.

Import is for the compiler to know which classes which class is the code referring.

I believe no diff will be there in bytecode if we are importing using * or individual classes
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic