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
This is the bytecode for the second:
public class java8.tests.General { public java8.tests.General(); Code: 0: aload_0 1: invokespecial #1
Maroun
source share