Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between complete package import .* and specified class import java? [duplicate]

Tags:

java

Possible Duplicate:
Why is using a wild card with a Java import statement bad?

Ex. 1

import javax.swing.*

JFrame f = new JFrame()

Ex. 2

import javax.swing.JFrame

JFrame f = new JFrame()

Is there any efficiency gain (even the slightest and minimal) in adapting 2) instead of 1) ? How does java does the referencing of packages internally?

The first time the compiler comes across the word JFrame, I presume that it should search for JFrame in complete swing.* package in case of 1)..Else if in case 2), it might probably get hold of the class directly by some indexing or may be key value hashing? So why is this not considered an efficiency gain even if it is tiny? (Please correct me if my presumptions about the internals are wrong)

EDIT :

Sorry for the duplicate.. Answer at Why is using a wild card with a Java import statement bad?

like image 576
Vamsi Emani Avatar asked Dec 16 '22 06:12

Vamsi Emani


1 Answers

There is no runtime penalty for using import javax.swing.* and import javax.swing.JFrame in Java. The only different is in compile time, the import package.* will search for whole package to find the correct class' information.

The Single-Type-Import (e.g., import javax.swing.JFrame) increases the readability of the program and it will be very clear which classes have been used.

The Type-Import-on-Demand (e.g. import javax.swing.*) causes the simple names of all public types declared in the package javax.swing to be available within the class and interface declarations of the compilation unit.

like image 176
Noufal Panolan Avatar answered Dec 18 '22 20:12

Noufal Panolan