Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which would make a Class file bigger? import java.awt.*, or a bunch or single import statements? [duplicate]

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.

like image 847
William Avatar asked Feb 25 '09 09:02

William


People also ask

Does import make my class bigger?

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.

Is it good to import * in Java?

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.

Why we should not import * in Java?

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.

What does import Java awt * mean?

import java. awt. Graphics means that the Graphics class in the java. awt package is made known to the current class. import java.


3 Answers

No difference. These are a compile-time construct only.

like image 181
Kent Boogaart Avatar answered Nov 10 '22 22:11

Kent Boogaart


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 importing 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 imports 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.

like image 23
coobird Avatar answered Nov 10 '22 21:11

coobird


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;
like image 40
Avi Avatar answered Nov 10 '22 22:11

Avi