• 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

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

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this...
 
Ajay Kamble
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit!

I got my answer!

Thanks,
Ajay
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ajay Kamble
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper for the nice answer.

Now I have better understanding of how imports work.

Regards,
Ajay
 
Ranch Hand
Posts: 40
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic