Burden of Java Libraries

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Burden of Java Libraries

Postby Rysto » Sun Oct 21, 2007 11:53 pm UTC

Java is dynamically linked. Using import has no effect whatsoever on the size of a .class file.

Karrion
Posts: 92
Joined: Fri Jun 22, 2007 12:14 am UTC
Location: Melbourne, AU

Re: Burden of Java Libraries

Postby Karrion » Sun Oct 21, 2007 11:58 pm UTC

Using a wildcard import could theoretically have a tiny compile speed penalty due to the compiler having to search all the class names in that package, but it's almost certainly negligible in any real world situation.

At runtime, the compiler has already done all the work and it won't make any difference whether you reference classes by their fully qualified name, import them, or wildcard import them.

julesh
Posts: 22
Joined: Wed Jul 04, 2007 9:20 am UTC

Re: Burden of Java Libraries

Postby julesh » Mon Oct 22, 2007 8:12 am UTC

My suggestion: use eclipse. It'll handle this for you automatically, choosing between individual class imports and full package imports based on how many classes you're using from each package.

User avatar
rrwoods
Posts: 1509
Joined: Mon Sep 24, 2007 5:57 pm UTC
Location: US

Re: Burden of Java Libraries

Postby rrwoods » Mon Oct 22, 2007 1:30 pm UTC

On using Eclipse -- is it possible to turn off automatic import statement generation? I prefer explicitly stating the library I'm using in 90% of situations, and sometimes one will slip by without me noticing.
31/M/taken/US
age/gender/interest/country

Belial wrote:The sex card is tournament legal. And I am tapping it for, like, six mana.

Waterhouse
Posts: 54
Joined: Wed Oct 03, 2007 12:37 pm UTC

Re: Burden of Java Libraries

Postby Waterhouse » Mon Oct 22, 2007 1:32 pm UTC

Wildcards in import statements can be misleading or confusing. If someone is reading your code and you refer to a class, it helps to state explicitly the full package name to the class in the import statement, so he will know where to look for reference.

The best route is, as mentioned, to use Eclipse, which makes this kind of thing trivial, but in some cases using an IDE isn't appropriate, such as in a university course that forbids them, for example.

|Erasmus|
Branson
Posts: 2643
Joined: Tue Oct 30, 2007 7:53 am UTC
Location: Sydney, Australia
Contact:

Re: Burden of Java Libraries

Postby |Erasmus| » Fri Nov 02, 2007 10:46 pm UTC

Meteorswarm wrote:Well, I'm coding this for me, and nobody else will work on the code, so clarity isn't THAT important. Additionally, the classes I'm using are java.util.Set, java.util.Map, the hash versions of each of these, and java.util.Iterator, which are all pretty standard, as far as I can tell. I'm using the Netbeans IDE, since it's what was on the computer, and my school has a totally paranoid permissions system on their computers, you have to get the computer techie to install stuff, not a teacher or the department heads, so getting a different IDE isn't feasible. I think I'll just go with the * import.

Thanks!


It may not make a speed difference, but the "correct" way, as far as I'm aware is to import specific packages.

Netbeans makes it very, very easy to do this with it's "fix imports" feature... It will add an import for each class you use.

Notch
Posts: 318
Joined: Tue Dec 12, 2006 5:52 pm UTC
Location: Stockholm, Sweden
Contact:

Re: Burden of Java Libraries

Postby Notch » Mon Nov 05, 2007 2:37 pm UTC

|Erasmus| wrote:It may not make a speed difference, but the "correct" way, as far as I'm aware is to import specific packages.


This is slightly off-topic, but I prefer to import entire packages instead. The reasoning is that if you're using a package, you might as well have access to the entire package, and if you run into name conflicts with your imports it's a sign that your class is doing too much, so split that sucker up!

(Unfortunately, this thinking kinda breaks down with java.awt.List and java.util.List, as it's pretty common for a class using awt to want to use collections..)

User avatar
Citizen K
Posts: 103
Joined: Fri Sep 21, 2007 11:42 am UTC
Location: Maryland

Re: Burden of Java Libraries

Postby Citizen K » Mon Nov 05, 2007 3:41 pm UTC

Waterhouse wrote:Wildcards in import statements can be misleading or confusing. If someone is reading your code and you refer to a class, it helps to state explicitly the full package name to the class in the import statement, so he will know where to look for reference.

The best route is, as mentioned, to use Eclipse, which makes this kind of thing trivial, but in some cases using an IDE isn't appropriate, such as in a university course that forbids them, for example.


1. I respectfully disagree. If you've written some sort of UI class that has to import two dozen other classes (layouts, labels, various fields, etc.), then nobody in their right mind wants to read through all that import mess when one or two wildcard imports would suffice. Everybody knows which JLabel you're talking about. And if they're using a reasonable IDE like any sensible person, a quick mouseover will pop up the full name of the class you're referencing anyway, without having to scroll up and look at a big block of import statements. Personally, the only time I ever do anything manual with an import statement is when there's some sort of name conflict like the List thing Notch mentioned. But those are few and far between.

2. There are courses using a high level language that forbid using IDEs? That seems quite silly. What exactly is the point of that? It's usually pretty obvious when the students are having it generate code instead of writing it themselves. And it won't generate code for anything actually interesting involved in the assignment anyway.
Beware the cows! Not all milk is enriched.

Waterhouse
Posts: 54
Joined: Wed Oct 03, 2007 12:37 pm UTC

Re: Burden of Java Libraries

Postby Waterhouse » Tue Nov 06, 2007 2:46 am UTC

Citizen K wrote:
1. I respectfully disagree. If you've written some sort of UI class that has to import two dozen other classes (layouts, labels, various fields, etc.), then nobody in their right mind wants to read through all that import mess when one or two wildcard imports would suffice. Everybody knows which JLabel you're talking about. And if they're using a reasonable IDE like any sensible person, a quick mouseover will pop up the full name of the class you're referencing anyway, without having to scroll up and look at a big block of import statements. Personally, the only time I ever do anything manual with an import statement is when there's some sort of name conflict like the List thing Notch mentioned. But those are few and far between.

I guess I can imagine a situation where, even though one programmer may be using an IDE so that they don't have to care about organizing their imports, another programmer might be reading it without that kind of aid. In practice, those kinds of things are indeed few and far between, but there are still a number of very talented people who prefer emacs or vi. Imagine reading some code which refers to another class "Widget." You see some method calls made, but want to know what they do. Obviously, if you have a modern IDE, you just click on the class name or whatever and you're transported to the file. But if you don't have such a thing at your fingertips, the next logical thing would be to look at the imports to find the full package name of the class:

Code: Select all

import org.a.*;
import org.a.utils.*;
import com.b.somethingforj.*;
import org.kitchen.sink.*;
import org.maze.twisty.passages.*;


Where does one look? A file system search can help, but not if the classes referenced are in a .jar file. Then you'd have to go looking through the expanded archive contents.

2. There are courses using a high level language that forbid using IDEs? That seems quite silly. What exactly is the point of that? It's usually pretty obvious when the students are having it generate code instead of writing it themselves. And it won't generate code for anything actually interesting involved in the assignment anyway.

Yes, there are (are were when I was in one of them). I can think of a few good reasons why an instructor might want to torture his students like that, although that may be the Stockholm Syndrome talking.

|Erasmus|
Branson
Posts: 2643
Joined: Tue Oct 30, 2007 7:53 am UTC
Location: Sydney, Australia
Contact:

Re: Burden of Java Libraries

Postby |Erasmus| » Wed Nov 07, 2007 11:11 am UTC

Notch wrote:
|Erasmus| wrote:It may not make a speed difference, but the "correct" way, as far as I'm aware is to import specific packages.


This is slightly off-topic, but I prefer to import entire packages instead. The reasoning is that if you're using a package, you might as well have access to the entire package, and if you run into name conflicts with your imports it's a sign that your class is doing too much, so split that sucker up!

(Unfortunately, this thinking kinda breaks down with java.awt.List and java.util.List, as it's pretty common for a class using awt to want to use collections..)


I'm talking more about doing things correctly in terms of the way Java defines they should be done, not in terms of practicality. And i believe the "correct" way is to use individual imports.

Also(@CitizenK), most IDE's you could use will let you hide the import statements anyway...

I'm yet to see any kind of Java standards recommend using wildcards in import statements.

Notch
Posts: 318
Joined: Tue Dec 12, 2006 5:52 pm UTC
Location: Stockholm, Sweden
Contact:

Re: Burden of Java Libraries

Postby Notch » Wed Nov 07, 2007 12:39 pm UTC

|Erasmus| wrote:I'm talking more about doing things correctly in terms of the way Java defines they should be done, not in terms of practicality. And i believe the "correct" way is to use individual imports.


A bit pedantic, but Java doesn't say anything about how it should be done, or it wouldn't allow import foo.*. ;)
The code conventions page on java.sun.com is some eight years old, and doesn't mention how import statements should look other than that they should follow immediately after the package statement.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Burden of Java Libraries

Postby Rysto » Wed Nov 07, 2007 7:34 pm UTC

|Erasmus| wrote:I'm yet to see any kind of Java standards recommend using wildcards in import statements.

I've yet to see any kind of Java coding standards that prohibit wildcards, either.

User avatar
Citizen K
Posts: 103
Joined: Fri Sep 21, 2007 11:42 am UTC
Location: Maryland

Re: Burden of Java Libraries

Postby Citizen K » Fri Nov 09, 2007 7:59 pm UTC

Yeah, I imagine this exact discussion is had every time some group of people tries to come up with a standard, and neither philosophy has enough of an advantage over the other to make a broad statement. Both are appropriate under the right circumstances.
Beware the cows! Not all milk is enriched.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 9 guests