aspose file tools*
The moose likes Beginning Java and the fly likes Difference between java.util.* and java.util.HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between java.util.* and java.util.HashMap" Watch "Difference between java.util.* and java.util.HashMap" New topic
Author

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

Kumara Sharma
Greenhorn

Joined: Sep 29, 2010
Posts: 6
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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

Joined: Aug 23, 2010
Posts: 49
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

Regards,
Sudipta Kumar Laha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between java.util.* and java.util.HashMap