This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

diffrence between package.* and package.ClassName

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic