问题
Okay, so if I had a project that used:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import java.awt.Polygon;
Would it make the Class file smaller to use:
import java.awt.*
I'm thinking against it because I'm importing a lot of things I don't need. I'm thinking for it cause it makes the file have a lot less characters.
回答1:
No difference. These are a compile-time construct only.
回答2:
No, there is no difference at all.
According to The Java Virtual Machine Specifications, Second Edition, Chapter 5: Loading, Linking and Initializing says the following:
The Java virtual machine dynamically loads (§2.17.2), links (§2.17.3), and initializes (§2.17.4) classes and interfaces. Loading is the process of finding the binary representation of a class or interface type with a particular name and creating a class or interface from that binary representation. Linking is the process of taking a class or interface and combining it into the runtime state of the Java virtual machine so that it can be executed.
At compile time, there are no linking of classes, therefore, using wildcards for import
ing makes no difference. Other classes are not included together into the resulting class
file.
In fact, if you look at the bytecode of the class
file (via javap or such disassembler), you won't find any import
statements, so having more or less numbers of import
statements in your source won't affect the size of the class
file.
Here's a simple experiment: Try writing a program, and compiling with import
s using wildcards, and another one with explicit imports. The resulting class
file should be the same size.
Using explicit import
statements on specific classes are perhaps less readable (and troublesome, if one doesn't use an IDE like Eclipse which will write it for you), but will allow you to deal with overlaps of class names in two packages.
For example, there is a List
class in both the java.util and java.awt packages. By importing both packages, there will be a conflict for the class named List
:
import java.util.*;
import java.awt.*;
// ... snip ... //
List l; // which "List" are we talking about?
By only importing the specific classes you need, these conflicts could be somewhat avoided:
import java.util.Hashmap;
import java.awt.List;
// .. snip ... //
List l; // Now we know for sure it's java.awt.List
Of course, if you had to use both java.util.List
and java.awt.List
then you're out of luck; you'll need to explicitly use their fully-qualified class names.
回答3:
The Java import
statement does not add any size to the class file. It allows the compiler to deduce what names refer to at compile time.
The only difference the line
import java.util.*;
makes is that you can write:
Set<String> set;
rather than:
java.util.Set<String> set;
回答4:
Honestly, why do you care about the size of your class file ?
It is a better practice to explicitly include all classes needed instead of doing a "import foo.bar.*".
That makes your code more readable, and more maintainable.
回答5:
I think that Java only includes that stuff you really need... So there won't be any difference in the final compiled version using one or the other way.
回答6:
No change at all in the generated class file.
Imports only have influence on the compiler (which might take a little bit longer if it has to index a lot of directories) and, of course, on the source code (which is IMO more maintainable when you can see exactly which classes are used).
回答7:
if you are interested in making the size of the class smaller, the game making competition java4k has some tips, e.g., this page http://www.ahristov.com/tutorial/java4k-tips/java4k-tips.html
回答8:
First, be sure to look at my "Import on Demand is Evil!" article:
http://javadude.com/articles/importondemandisevil.html
You should never use the * syntax! It can cause your program to stop compiling when code is added to another library!
Now here's how import really works:
When you say
import a.b.c.Foo;
all this does is tell the compiler that when you say "Foo", you really mean "a.b.c.Foo". The compiler will then replace all occurences of Foo with a.b.c.Foo in the generated bytecode.
When you say
import a.b.c.*;
this tells the compiler "when you see a symbol XXX you don't recognize, check to see if there is an a.b.c.XXX" If found, the compiler will replace XXX with a.b.c.XXX in the generated bytecode.
If more than one "*" import matches an unknown symbol, you'll get a compiler error for the ambiguity.
回答9:
There is no difference as stated in previous posts, however as a preference I use strict importing as I find it makes in my opinion the code more pleasent to read
回答10:
In complement to earlier answers, the only difference you should see is that the compiling should be a bit faster, when you explicitly tell what you want import
ed, instead of giving a wildcard, that can include thousands (upon thousands) of unnecessary packages and classes.
来源:https://stackoverflow.com/questions/585268/which-would-make-a-class-file-bigger-import-java-awt-or-a-bunch-or-single-im