Forums Register Login

diffrence between package.* and package.ClassName

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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();
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 843 times.
Similar Threads
XSL and XSLT
core java,purpose of volatile and transient
doubt about wrapping and boxing
Diff between Element and node
Please anwer to my question?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:50:51.