Difference between using full name and import in Java

Are there any differences using the "built-in import" (full name) and regular 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(".")); 
+7
java import inline
source share
6 answers

The main thing you should pay attention to is readability. I think the second one is read.

In rare cases, I prefer the second approach. Consider the following scenario: for some reason, I wrote a class and named it File . I typed File file = new File(...) and my IDE automatically imported java.io.File for me. But I do not want such an object, I want my class File . So instead of importing the correct class, I prefer inline-import, just so that other users do not get confused with the Java File class.

As for performance, they are exactly the same, and here's the proof -

This is the bytecode generated for the first fragment:

 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 } 
+9
source share

There are no identical parameters of productivity, memory, compilation time. The only difference between the two is that normal import saves you text input efforts and is more readable than "it."

+4
source share

This only matters in the following case:

If you want to use a class that is available in multiple packages. those. date class is available in java.util and java.sql

You can import one of the above classes at the beginning of the program and use qualified syntax for the other.

+3
source share

If you import classes with the same name into one class, you can explicitly specify which one to use (full class name):

 import java.io.File; ... chooser.setCurrentDirectory(new File(".")); // java.io.File new org.yourpackage.File("sdfsdf"); // org.yourpackage.File 
+2
source share

No, this will not affect the performance of your code. Import statements make your code more readable because you are not writing all of your package name. But sometimes you have conflicts with class names, then it is advisable to use qualified names.

+2
source share

In java classes, their full name in the last bytecode is always referenced. This happens the first time a class object is created (or a static class member is available).

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

Thus, there is a 0 difference between the import of the class or the full name is explicitly indicated - this is just a matter of readability, preserves some typing and helps prevent collisions between classes with the same name.

+1
source share

All Articles