5 Replies - 248 Views - Last Post: 03 October 2012 - 06:45 PM Rate Topic: -----

#1 HapticThreek  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-October 11

Advice about using imports

Posted 03 October 2012 - 05:59 AM

Hello folks,

So in the my first java file I have these imports:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


and in my second i have these:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


The reason I have done it like this instead of doing

import java.io.*;
import java.util.*;
import java.util.regex.*;


Is because I figured that it was more efficient to import only the exact things you are using instead of importing all the classes and only using a couple of them. Is this the best way to do it or am I just making my code look a bit long winded and sloppy?

Appreciate all of your opinions :)

Is This A Good Question/Topic? 0
  • +

Replies To: Advice about using imports

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7950
  • View blog
  • Posts: 13,543
  • Joined: 19-March 11

Re: Advice about using imports

Posted 03 October 2012 - 04:33 PM

Java will only import what is used, so it's not a matter of efficiency.
However, it's still a good idea to use the explicit imports. This prevents namespace collisions (if two libraries have classes with the same names, it makes a mess) and it also gives whoever's reading your code an idea of what to expect.

There's another purpose to it, a diagnostic purpose: I usually find that if you have a huge list of imports, it probably means your class is too large and trying to do too much. Your import lists look reasonable, not out of control, but one thing I look for is, for example GUI classes mixed in with file IO or net classes - that's usually a warning sign from the beginning.
Was This Post Helpful? 2
  • +
  • -

#3 HapticThreek  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-October 11

Re: Advice about using imports

Posted 03 October 2012 - 04:43 PM

View Postjon.kiparsky, on 03 October 2012 - 04:33 PM, said:

Java will only import what is used, so it's not a matter of efficiency.
However, it's still a good idea to use the explicit imports. This prevents namespace collisions (if two libraries have classes with the same names, it makes a mess) and it also gives whoever's reading your code an idea of what to expect.

There's another purpose to it, a diagnostic purpose: I usually find that if you have a huge list of imports, it probably means your class is too large and trying to do too much. Your import lists look reasonable, not out of control, but one thing I look for is, for example GUI classes mixed in with file IO or net classes - that's usually a warning sign from the beginning.


Thanks for the reply, and the information! I thought it would import everything every time.
Was This Post Helpful? 0
  • +
  • -

#4 lilVaratep  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 218
  • Joined: 09-October 11

Re: Advice about using imports

Posted 03 October 2012 - 05:35 PM

View PostHapticThreek, on 03 October 2012 - 04:43 PM, said:

Thanks for the reply, and the information! I thought it would import everything every time.


There's also something called the garbage collector that takes out everything unused (without us seeing it being removed - even variables and objects!)
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7950
  • View blog
  • Posts: 13,543
  • Joined: 19-March 11

Re: Advice about using imports

Posted 03 October 2012 - 05:48 PM

That's a little different business. The garbage collector cleans up the heap, by clearing out unused references to objects. This is a way of preventing memory leaks.

When you're talking about importing classes into your code, you're talking about increasing the overall footprint of the object you're creating. For example, if the import statement
import java.util.*

meant "include the code for all of the objects in the util library", then it would bloat a simple application beyond belief. As I said, this does not happen.

So the OP's concern was about making the objects too big, while the garbage collector makes sure that the objects don't stick around after they're no longer needed.
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Advice about using imports

Posted 03 October 2012 - 06:45 PM

View PostHapticThreek, on 03 October 2012 - 08:59 AM, said:

Is because I figured that it was more efficient to import only the exact things you are using instead of importing all the classes and only using a couple of them.


In the early 80's when we use to fire a compilation before leaving the office at 5h00 because it usually took 12 hours to perform a full compile on a Z80 I used to worry for that.

With actual PCs who cares if your compilation 1.000 or 1.002 seconds ?

The decison of using single or multiple import should be only based on documentation purposes.
Unless you really want to show that you use a single IO class or a single swing component don't really see any reason not to use the * form
unless you have two classes with the same name and that the import * generates a conflict.

In any case, the .class (the binary generated) is exactly the same. The import is just a dictionary after all but may be for a few static variables, I wouldn't bother about them

This post has been edited by pbl: 03 October 2012 - 07:02 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1