If I access a class within a package using fully qualified name, without importing it, whether it saves any memory?
Using fully qualified class name :
java.lang.Math.sqrt(x);
Import package :
import java.lang.Math;
Math.sqrt(x);
which is the better way : import the package or access using fully qualified name?
Thanking you..
To import the java package into a class, we need to use the java import keyword which is used to access the package and its classes into the java program. Use import to access built-in and user-defined packages into your java source file to refer to a class in another package by directly using its name.
Explanation: The import keyword is used to access the classes and interfaces of a particular package to the current file.
The only way to access classes in the default package is from another class in the default package. In that case, don't bother to import it, just refer to it directly. This is actually the right answer to the question.
There is no performance difference between importing the package or using the fully qualified class name. The import directive is not converted to Java byte code, consequently there is no effect on runtime performance. The only difference is that it saves you time in case you are using the imported class multiple times. This is a good read here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With