Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between using fully qualified name and import in Java

Are there any differences using an "inline import" (a fully qualified name) and normal import in terms of performance, memory, compile-time, etc. in Java?

chooser.setCurrentDirectory(new java.io.File("."));

and

import java.io.File;
...
   chooser.setCurrentDirectory(new File("."));
like image 702
uguurozkan Avatar asked Oct 06 '15 09:10

uguurozkan


People also ask

Can we give fully-qualified class name instead of importing the class?

3) Using fully qualified name Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface. It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class.

What is use of fully qualified name in Java?

A fully-qualified class name in Java contains the package that the class originated from. Also, an inner class is a class that is another class member. So, the fully-qualified name of an inner class can be obtained using the getName() method.

Which is a risk of using fully-qualified class name when importing?

Explanation: Memory usage is increased. The compiler runs longer. Performance of the code is reduced.

What is difference between import and package in Java?

Package is used to put all one Module related into one specified Folder for a better understanding ,whereas import is used to import the specific class that we need to run our application/class(Like we use Java. Util...etc).


2 Answers

The main thing you should focus in is readability. I find the second one more readable.

In rare cases, I prefer the second approach. Let's consider the following scenario: For some reason, I wrote a class and named it File. I typed File file = new File(...) and my IDE auto-imported the java.io.File for me. But I don't want that kind of object, I want my File class. So instead of importing the correct class, I prefer inline-import it, just that other users won't get confused with the Java's File class.

Regarding the performance, they're exactly the same, and here's the proof -

This is the bytecode generated for the first snippet:

public class java8.tests.General {
  public java8.tests.General();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class javax/swing/JFileChooser
       3: dup
       4: invokespecial #3                  // Method javax/swing/JFileChooser."<init>":()V
       7: astore_1
       8: aload_1
       9: new           #4                  // class java/io/File
      12: dup
      13: ldc           #5                  // String .
      15: invokespecial #6                  // Method java/io/File."<init>":(Ljava/lang/String;)V
      18: invokevirtual #7                  // Method javax/swing/JFileChooser.setCurrentDirectory:(Ljava/io/File;)V
      21: return
}

This is the bytecode for the second:

public class java8.tests.General {
  public java8.tests.General();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class javax/swing/JFileChooser
       3: dup
       4: invokespecial #3                  // Method javax/swing/JFileChooser."<init>":()V
       7: astore_1
       8: aload_1
       9: new           #4                  // class java/io/File
      12: dup
      13: ldc           #5                  // String .
      15: invokespecial #6                  // Method java/io/File."<init>":(Ljava/lang/String;)V
      18: invokevirtual #7                  // Method javax/swing/JFileChooser.setCurrentDirectory:(Ljava/io/File;)V
      21: return
}
like image 142
Maroun Avatar answered Sep 17 '22 13:09

Maroun


No both are exactly same in the terms of performance, memory, compile-time. The only difference between them is that normal import save your typing efforts and it's more readable that' it.

like image 42
Gaurav Jeswani Avatar answered Sep 17 '22 13:09

Gaurav Jeswani