• Post Reply Bookmark Topic Watch Topic
  • New Topic

diffrence between package.* and package.ClassName  RSS feed

 
Raghuveer Rawat
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
In java we can import either whole package(java.util.* ) or only those classes that need to be included (java.util.ArrayList, java.util.Map etc )
so what will be the performance diffrence between these two ways.
will compilation time will be more in first case
or runtime execution time will be mroe in first case.
or first way will take more momory than second way.

Thanks
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import mypackage.*;
vs
import mypackage.MyClass1;
import mypackage.MyClass2;
...
has no discernable affect on performance, memory, compile time.
import mypackage.* doesn't neccessarily mean that all of the classes in mypackage are loaded by the JVM, only those that you actually use will be loaded.
eg. when you have a statement such as ArrayList list = new ArrayList();
If anything, importing each class by name "might" be a bit slower on compile time because there is more lines in your text file, but it really wouldn't be anything that would be noticable.
So no, the 2 choices of imports do not affect performance.
 
Raghuveer Rawat
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If anything, importing each class by name "might" be a bit slower on compile time because there is more lines in your text file, but it really wouldn't be anything that would be noticable.

I guess importing each class by name should reduce the time in searching the these classes from the whole package.
 
Lei Wu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the usage java.util.* will cost theoretically a little more time in compilation. For example, if you write code like this:

A compiler must take additional actions to make sure that only one package contains class MyClass. If class with the same name 'MyClass' appears in both packages, it will produce a compile-time error. So it may check out all the packages to ensure the unique appearance of class MyClass.
If it is written like:

No such extra actions will be required.
Of course this time cost is indiscernible.
Another, but more important, difference is that they have different precedence.
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just give a thought about this.
I have a class name TestForPackage inside package com.package.test1
and another class TestForPackage inside package com.package.test2
Assume your class import using following statement.
import com.pagackage.test1.*;
import com.pagackage.test2.*;
and use TestForPackage class. Now try to correlate the need for selective import.
-Arun
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The biggest difference is when you have classes in 2 packages with the same name.
For example p1.Class1 and p2.Class1
If you need both packages but not p2.Class1 you should not import p2.* to avoid confusion about which Class1 you're declaring in Class1 i = new Class1();
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are correct.
Check this

now try to use the Date class. Compiler will complain "reference to Date is ambiguous. Because Date class is available in both packages. If you are particular which Date class you are using, then no compile time error.
for example

-------------
Sainudheen
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I strongly prefer having the classname on the import. It avoids unexpected classname conflicts and is good documentation of exactly what you're using. Eclipse can be set to warn you about unused imports, and can change .* into .class at the click of the mouse. That makes me smile.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!