1

I'm wondering if there is any difference between something like the following implementations:

import java.util.Date;

public class SimpleDatePrinter {

    public void printDate() {
        System.out.println(new Date());
    }

}

... and ...

public class SimpleDatePrinter {

    public void printDate() {
        System.out.println(new java.util.Date());
    }

}

The reason I ask is because my understanding from C++ include statements is that the contents of the included file are basically copied into the source file at compile time. I'm unsure if import statements in Java work in the same way, but if they do, would using the second construction shown above possibly save memory (since you are not importing the entire java.util.Date class into the SimpleDatePrinter? Or is this irrelevant?

I realize that writing code without ever importing a class would be detrimental to readability and whatnot. I also realize that in the example above it's "not enough to worry about." I'm just curious about this for cases in which performance is a crucial factor.

asteri
  • 11,402
  • 13
  • 60
  • 84
  • 1
    Java is not C++ and how Java imports work is a simple Google search away –  Jul 18 '13 at 15:31
  • @JarrodRoberson That still wouldn't answer the question. It's possible that the second construction implicitly imports the class. Hence the question. – asteri Jul 18 '13 at 15:32
  • My point is how imports work is finely detailed in the Oracle documentation and is very easy to find detailed explanations and descriptions of the mechanics if you did some basic investigation. Heck even taking the minimum time to look at the suggested duplicates of this question would show a little bit of effort. http://stackoverflow.com/questions/1053658/how-is-import-done-in-java –  Jul 18 '13 at 15:33
  • 2
    @Jarrod Roberson isn't it more polite to simply answer the question first then direct the OP where he can get more detailed explanations? Sometimes the answer is only a Google search away but the right query is never obvious. – qualebs Jul 18 '13 at 15:48

2 Answers2

5

No, there is no difference. import statement is used to avoid using fully qualified names of the class we are using. The documentation doesn't talk anything about performance improvement

sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • Yep, the only "savings" from import is in the number of characters you have to type. And the "cost" associated with doing so is the potential for confusion with conflicting names. The exact same .class file is generated in either case. – Hot Licks Jul 18 '13 at 15:35
  • 1
    +1 The byte code will be exactly the same, so there is no difference. – Peter Lawrey Jul 18 '13 at 15:46
  • Thanks for the link. I looked at that before asking the question, but as you say, it doesn't say anything about the difference between using the fully-qualified name and importing, if there is on at all. – asteri Jul 18 '13 at 15:49
5

Imports are resolved at compile time. In your example, the generated bytecode will be the same. And at runtime, the class (Date) will need to be loaded in any case. So it makes no difference from a performance perspective.

assylias
  • 321,522
  • 82
  • 660
  • 783