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.
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. Save this answer.
Java developers use the wildcard (*) in import statements to import all the classes in a particular package. But in code reviews, most of you may have asked to remove those wildcard imports and add the actual class name.
class file. Without the wildcard(*), the directive tells the compiler to look for one specific file in the classpath. Thus, it is suffice to say that the import directive may influence the compile time but it does not affect the runtime of the program.
import java. awt. Graphics means that the Graphics class in the java. awt package is made known to the current class. import java.
No difference. These are a compile-time construct only.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With