• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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.
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic