Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Disadvantage of using wildcards in import statement.

Thread view: 
Dipankar - 08 Sep 2006 05:46 GMT
Hi,

Recently I wrote a certification test for Java2. There was a question
on using wildcards in import statement with four options. Only one
among them is correct (as per the test paper).
I was not very sure about the answer.

Can anyone help me in finding the answer?

Question

import java.util.*;

What is the disadvantage of using "*" in an import statement as shown
in the sample above?

Choice 1   :  The class file is less portable.
Choice 2   :  The Java Virtual Machine takes slightly longer to
validate the class file.
Choice 3   :  The maximum number of classes allowed to be imported is
limited to 128.
Choice 4   : The code dependencies are difficult to evaluate from
reading the code.
Choice 5   : The Java Virtual Machine takes slightly longer to load the
class file.

According to me the Choice 4 is correct. But I am not sure about the
impact on JVM while using wildcards.

Thanks in advance.
jiji - 08 Sep 2006 06:04 GMT
Hi,

wildcards in import statement may slow down the compilation process. It
doesnt have any impact on performance of the application at run time.

Actually, the imported classes bytecode wont be bundled or included
with ur class. the import is more for resolving the actual class u r
refering in ur code..

~Jiji
Patricia Shanahan - 08 Sep 2006 06:05 GMT
> Hi,
>
[quoted text clipped - 26 lines]
>
> Thanks in advance.

Java import statements are a compile time mechanism for mapping a simple
name such as "List" to a qualified name such as "java.util.List". I'm
not aware of any impact at all on the JVM.

Patricia
Chris Smith - 08 Sep 2006 08:58 GMT
> Java import statements are a compile time mechanism for mapping a simple
> name such as "List" to a qualified name such as "java.util.List". I'm
> not aware of any impact at all on the JVM.

In fact, the JVM class file specification leaves little room for
compiled binary code to even contain any indications of what imports
were used in the source.  Of course one can't specify that the
performance has to be equal -- so technically it's not guaranteed -- but
it would require something malicious for a compiler to actually generate
slower code due to imports.

Signature

Chris Smith

Neil Padgen - 08 Sep 2006 14:22 GMT
> Hi,
>
[quoted text clipped - 26 lines]
>
> Thanks in advance.

It's not just that code dependencies are difficult to evaluate: your
code might suddenly fail to compile.

Consider the following:

/* Bar.java */
package foo;

import java.util.*;

public class Bar {
    public void baz() {
        List ambiguousList = new ArrayList();
    }
}
/* end Bar.java */

This compiles fine ... until I create a new class List in package foo.

$ javac *.java
Bar.java:7: incompatible types
found   : java.util.ArrayList
required: foo.List
        List ambiguousList = new ArrayList();
                             ^
1 error

-- Neil
ralf@rw7.de - 10 Sep 2006 07:34 GMT
> It's not just that code dependencies are difficult to evaluate: your
> code might suddenly fail to compile.

Some long time ago I worked in a project that had it's own class
Currency. Then came JDK 1.4 and it's new class java.util.Currency. And
of course there were "import java.util.*" everywhere. We had to fix a
few hundert source files.

We used a script for that task (thanks sed), but it wasn't nice.

I think, with modern Java IDEs managing imports almost automatically,
there is no sound reason for using wildcard imports anymore.

Best regards,
Ralf.
Arne Vajhøj - 10 Sep 2006 18:50 GMT
> Some long time ago I worked in a project that had it's own class
> Currency. Then came JDK 1.4 and it's new class java.util.Currency. And
[quoted text clipped - 5 lines]
> I think, with modern Java IDEs managing imports almost automatically,
> there is no sound reason for using wildcard imports anymore.

Not in production code.

Code snippets to news groups maybe.

:-)

Wildcard imports should get shot down at the code
review.

Or even better by an automatic style checker run before
the code get into code review.

Arne
Daniel Dyer - 10 Sep 2006 18:57 GMT
> Wildcard imports should get shot down at the code
> review.
>
> Or even better by an automatic style checker run before
> the code get into code review.

IDEA has the option to optimise imports when it commits any changes to  
version control.  Your imports will be updated to match your configured  
preferences (so expand wildcards and remove unused imports).  Presumably  
Eclipse has something similar (and NetBeans too)?

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Arne Vajhøj - 10 Sep 2006 19:05 GMT
> IDEA has the option to optimise imports when it commits any changes to
> version control.  Your imports will be updated to match your configured
> preferences (so expand wildcards and remove unused imports).  Presumably
> Eclipse has something similar (and NetBeans too)?

Interesting.

But I am so oldfashioned that I do not like if my code
is changed behind my back by a tool.

Arne
IchBin - 10 Sep 2006 22:40 GMT
>> IDEA has the option to optimise imports when it commits any changes to
>> version control.  Your imports will be updated to match your
[quoted text clipped - 7 lines]
>
> Arne

Well I hate to tell you this but Eclipse and NetBeans can also
automatically build your import statements.

I am old fashioned too but it nice to use these tools for at least when
you open the program you can tell right upfront what classes the code is
using. For Eclipse it has been able to do this for more than two years.
Netbeans just added to their IDE.

So it is a proven functional part of a professional IDE.

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA              http://weconsultants.phpnet.us
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

Arne Vajhøj - 11 Sep 2006 00:07 GMT
>>> IDEA has the option to optimise imports when it commits any changes
>>> to version control.  Your imports will be updated to match your
[quoted text clipped - 15 lines]
>
> So it is a proven functional part of a professional IDE.

I know and I use that feature all the time.

But it has very little in common with what I commented on.

I do not have a problem with the editor proposing
something that I can view and modify if I want to.

I have a problem if I look at the code, I think it is OK
and I choose commit and then the editor change the code.

Arne

co
Karl Uppiano - 11 Sep 2006 07:15 GMT
>>>> IDEA has the option to optimise imports when it commits any changes to
>>>> version control.  Your imports will be updated to match your configured
[quoted text clipped - 29 lines]
>
> co

NetBeans does not automatically change import statements, but it will
generate them for you, and arrange them alphabetically. I never type import
statements by hand anymore. Net beans used to collapse down to wildcards
after 5 (configurable) imports from the same package. I don't think they do
that anymore.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.