The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Originally posted by somkiat puisungnoen:
In my opinion,
import pkg.class;
It's better than import pkg.*;
Because your code is readability, easy to maintain code.
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
SCJP 1.4, SCDJWS , SCJA<br />I can do ALL things through CHRIST who strengthens me.
Originally posted by Arun Prasath:
yes, I dont think there is any performance difference between the two.
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Originally posted by Don Kiddick:
It will take longer to compile though
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Originally posted by somkiat puisungnoen:
Remark On IDE::
Organized import ::
- Eclipse use full import like java.util.Date;
- JBuilder use * import like java.util.*;
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Work like you don't need the money. Love like you've never been hated. Dance like nobody's watching. Sing like nobody's listening. Live like it's Heaven on Earth.
Currently I Reside Here WEBlog
Tony Morris
Java Q&A (FAQ, Trivia)
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Stefan Wagner:
Did someone ever measure the performance, which is told to be smaller when using import foo.* ?
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Tony Morris
Java Q&A (FAQ, Trivia)
Does using * in an import statement affect performance?
This question has come up a surprising number of times in various forums. The question relates to the import directive, as in
import java.util.*;
The short answer is: no, there is no affect on runtime performance.
The import directive is a compiler directive. The Java source to bytecode compiler reads the Java source file (i.e. a something.java file) and converts that source to one or more bytecode files (.class files).
Most Java source code in the Java source file is converted into Java bytecodes of one sort or another. But the import directive is not. The import directive specifically tells the compiler to do something. The import directive tells the compiler that when it comes across a class or interface name in the source file, e.g. HashMap, then if it cannot find the class file for that name, it should use the import statement to help it look it up. This way, you don't have to always use fully qualified class names, e.g. java.util.HashMap.
The import directives themselves do not get put into the compiled bytecode files. They are no longer of any use, as the compiler compiles the fully qualified name into the .class file.
For example, suppose the source file contains
import java.util.*;
//
...
HashMap map;
...
Then the compiler gets to the HashMap map variable declaration, tries to find a class called HashMap in the default package (i.e. no package name), fails, and uses the import statement to see if there is a java.util.HashMap class. This is found, and a reference to java.util.HashMap is inserted into the .class file. After compilation is completed, there is no use for the import directive, so it is not present at all in the compiled bytecode. Consequently, the import directive cannot affect runtime code execution in any way.
However, it is worth noting that the directive does affect compilation time. Since the directive tells the compiler how to do a second lookup to find classes, obviously this means that more time is spent looking up class and interface names compared to if one lookup sufficed.
In fact, there are two forms of the import directive: with and without the wildcard '*', e.g.
import java.util.*;
import java.util.HashMap;
Without the wildcard, the directive tells the compiler to look for one specific file in the classpath. With the wildcard, the directive is telling the compiler to look for the named package, and to search in that package for possible matches everytime any name needs to be matched. This latter version is probably going to be more costly (i.e. take longer) for the compiler than the former.
Also, depending on which compiler you use, and which settings, the compiler may re-compile all of the classes in the wildcard package, which could really make things take longer.
Finally, many people find that using imports with wildcards makes the source much less readable, since the reader also needs to figure out which package a particular class comes from, rather than just looking it up in the import statement.
So the full answer is
* There is no runtime cost from using an import statement
* The compilation process can take a little more time with an import statement
* The compilation process can take even more time with a wildcard import statement
* For improved readability, wildcard import statements are bad practice for anything but throwaway classes
* The compilation overhead of non-wildcard import statements are minor, but they give readability benefits so best practice is to use them
SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
So the full answer is
* There is no runtime cost from using an import statement
* The compilation process can take a little more time with an import statement
* The compilation process can take even more time with a wildcard import statement
* For improved readability, wildcard import statements are bad practice for anything but throwaway classes
* The compilation overhead of non-wildcard import statements are minor, but they give readability benefits so best practice is to use them
Tony Morris
Java Q&A (FAQ, Trivia)
Did someone ever measure the performance, which is told to be smaller when using
import foo.* ?
3000 Classnames can be effectively be stored in a hash.
And parsing multiple 'import' - statements isn't for free.
I guess the statement needs some proof.
Also, depending on which compiler you use, and which settings, the compiler may re-compile all of the classes in the wildcard package, which could really make things take longer.
When I don't know where SomeNonCoreClass or SomeOtherClass comes from, I right-click on my mouse in eclipse, and I know, while you're scrolling up to find the import-statement.
Giving the judge back to you.
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Tony Morris:
And if the source (or even binaries) to these classes is not available (as is often the case)?
What does Eclipse tell you then?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ilja Preuss:
As this is a recurring question, I created a FAQ entry: http://faq.javaranch.com/view?WildCardVsSpecificImports
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Felipe Carasso wrote:Reviving this old discussion.
I came across a situation where a class uses constants from another class for text labels. These classes are tightly connected.
What would you prefer?
import static com.something.something.something.something.something.something.something.Label1
import static com.something.something.something.something.something.something.something.Label2
(...) x20
or...
import static com.something.something.something.something.something.something.something.*
?
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
Paper jam tastes about as you would expect. Try some on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|