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.
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.
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.
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.
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
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();
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
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding: