GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Is import java.util.List faster than java.util.* Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is import java.util.List faster than java.util.*" Watch "Is import java.util.List faster than java.util.*" New topic
Author

Is import java.util.List faster than java.util.*

Ajay Kamble
Ranch Hand

Joined: Jan 23, 2009
Posts: 51
Hello,

Is it true that java.util.List (importing a specific class) will make a program faster than java.util.*, or is this just a myth?

I know that import is just a way to say which List we want to use [java.util.List or com.abc.List] and we are not actually loading anything in JVM.

It would be great if I can get answer of above question and details about how JVM processes import statements or perhaps some resource which explains this.

Will JVM maintain a table in memory containing all types that are imported [or available to load]?

Regards,
Ajay
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

Is it true that java.util.List (importing a specific class) will make a program faster than java.util.*, or is this just a myth?


Differenc is import java.util.List will just import List class where as import java.util.* will import all the classes which are there is uil package.

So always use java.util.List instead of java.util.*



know that import is just a way to say which List we want to use [java.util.List or com.abc.List] and we are not actually loading anything in JVM.

when we say import java.util.List then this class will also get loaded by classLoader in the memory.

Ajay Kamble
Ranch Hand

Joined: Jan 23, 2009
Posts: 51
This difference is pretty obvious.

My question was is one faster than the other? How JVM will deal with two imports?

You are wrong when you say that import is going to load the class in memory!!

Regards,
Ajay
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

My question was is one faster than the other? How JVM will deal with two imports?


You tell which would be faster then ? loading just one class or entire package ?



How JVM will deal with two imports?

Everything after compilation is done by JVM. What in particular you want to know about JVM?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Read this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ajay Kamble
Ranch Hand

Joined: Jan 23, 2009
Posts: 51
You need to understand that IMPORT is not going to load class in memory!!

JVM will load the class only when it reads through program and encounters a class which is not already loaded in memory.

Regards,
Ajay
Ajay Kamble
Ranch Hand

Joined: Jan 23, 2009
Posts: 51
Thanks Ankit!

I got my answer!

Thanks,
Ajay
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

Ajay Kamble wrote:My question was is one faster than the other? How JVM will deal with two imports?

One is not faster than the other.

Note that import statements are only directives for the compiler, to tell the compiler in which packages it can find classes that you use. It makes no difference in the bytecode that the compiler produces whether you import java.util.List or java.util.*.

You can check this yourself: write a small program with import java.util.List, compile it, and look at the bytecode (you can use the javap tool included with the JDK to disassemble the class file). Then change the import to java.util.*, compile and disassemble again. You'll see that the bytecode is exactly the same.

Whether one is "faster" than the other is not a reason to choose one over the other. There are, however, perfectly good other reasons to import java.util.List instead of java.util.*. The most important one is that importing java.util.* can make your program incompatible with a future Java version:

Suppose that you have a class named "Something", you're using JDK 6 and you import java.util.*. Now, suppose that Java 7 comes out, and it has a new class in package java.util that by coincidence also happens to be called "Something". Your program now will not compile on Java 7, because the compiler doesn't know if with the name "Something" you mean your own class or the new class in java.util.

You won't have that problem when you import only those classes from java.util that you actually use.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ajay Kamble
Ranch Hand

Joined: Jan 23, 2009
Posts: 51
Thanks Jesper for the nice answer.

Now I have better understanding of how imports work.

Regards,
Ajay
Parambir Singh
Ranch Hand

Joined: Sep 05, 2004
Posts: 40

Importing a whole package can only make the compilation slower, not the execution of the program. The compiler searches for a class that you have used in your program in the classes/packages you have imported. Therefore, the more classes it has to search in, the more time it is going to take. However, I haven't seen it make much of a difference practically. It is more of a theoretical difference (in my experience).

What can affect you as a developer is the fact that importing whole packages can create unnecessary conflicts. E.g. there's a Date class in both java.util and java.sql package. If you are using some other classes from both these packages and instead of importing specific classes (java.util.Calendar), you import the whole package (java.util.* & java.sql.*), you can't simply use the Date class. You'll have to use the fully qualified name of the class (e.g. java.util.Date).


SCJP, SCMAD
 
GeeCON Prague 2014
 
subject: Is import java.util.List faster than java.util.*