• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Re: Java Import Statement

 
Joe Cheung
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

In terms of performance on compilation or run-time, is it strongly recommended that programmers should use import statement for the required classes only like using "import java.util.Map;" rather then using it for the whole package such as "import java.util.*;"? If so, what is the rationale behind?

Thanks!
Joe
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes absolutely no performance difference at runtime, and no observable difference at compile time. Where it matters is not performance, but in the understandability and maintainability of the code. If you use very specific imports, or so the theory goes, then the person reading your code can very quickly understand where each class that's referred to comes from.

Personally, I find this rationale very weak in these days of powerful IDEs. If you're reading code in an IDE, it can tell you instantly what package any given class belongs to in an instant.

Another slightly more defensible position says that using very specific imports makes it clear what dependencies a class has. Again, tools can tell you this same information, but the imports do give you a good idea, at a glance.
 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know whether I can rise a question in this thread.

Even If we are using import java.util.*;, the JIT compiler will compile the class that we are required and not all the class in util. Whether it is true?
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't have anything to do with JIT. The import statements are just a direction for the compiler to find which class to use. Importing a "whole package" makes no difference in the resulting bytecode than importing a single class.

Creating and Using Packages
 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a general question: Why we are using JIT compiler. Whether it has been included in JRE? What is the significance of using JIT compiler?
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we are using JIT compiler. Whether it has been included in JRE? What is the significance of using JIT compiler?
The Java HotSpot Virtual Machine, v1.4.1

It's a long read, but you can skip forward to page 4...
[ July 08, 2005: Message edited by: Steve Morrow ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic