Difference between using fully qualified name and import in Java

ぃ、小莉子 提交于 2019-12-05 05:37:28

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
}

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.

No, it wouldn't affect performance of your code. Import statements make your code more readable because you're not writing all your package name. But sometimes, you have a conflict of ClassNames, then it is advisable to use qualified names.

It only matters in following case,

If you want to use a class which is available in multiples packages. i.e. Date class is available in java.util & java.sql

You can import one of above class at start of program, and use qualified syntax for another one.

If you are importing classes of the same name in one class, you are able to explicitly tell which one to use where (full class name):

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

new org.yourpackage.File("sdfsdf");         // org.yourpackage.File

In java classes are always referenced by their fully qualified name in the final bytecode. It happens the very first time when an object of class is created (or a static member of the class is accessed).

That said, import is used by the compiler to access classes by their unqualified name (MyClass instead of mypackage.MyClass).

So there is a 0 difference between importing the class, or writing explicitly the fully qualified name - it's just matter of readability, saves some typing and helps to prevent clashing between classes with a same name.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!