This article says No More Typedefs, Defines, or Preprocessor in 2.2.1 . In C++ the include
is part of the preprocessor. What is the import
?
Java does not have any form of the C #ifdef or #if directives to perform conditional compilation. In theory, conditional compilation is not necessary in Java since it is a platform-independent language, and thus there are no platform dependencies that require the technique.
Java has an import statement that allows you to import an entire package (as in earlier examples), or use only certain classes and interfaces defined in the package. The import statement is optional in Java.
A preprocessor is a program that works on the source before the compilation. As the name implies, the preprocessor prepares the source for compilation. The notion of the preprocessor has been there from the earliest times of programming languages.
Import statement in Java is helpful to take a class or all classes visible for a program specified under a package, with the help of a single statement. It is pretty beneficial as the programmer do not require to write the entire class definition. Hence, it improves the readability of the program.
Import despite the name doesn't "import" anything, it just let you call the classes without the fully qualified name.
To clarify, if I do an import java.util.ArrayList;
, now I can refer to ArrayList
class as just ArrayList
. If I don't do it, I can still use the class, I just have to call it java.util.ArrayList
.
If you import whole packages with *
, the worst thing it can happen is that there is a name clash, thus, you've to use the full name to refer to a Java class, but it doesn't use more memory at runtime.
Classes in java.lang
are automatically "imported".
Java 1.5 introduced static imports, which enables programmers to refer to imported static members as if they were declared in the class that uses them. They should be used sparingly. An acceptable use is for importing JUnit Assert methods. For instance, with a traditional import:
import org.junit.Assert;
...
Assert.assertEquals(expected, actual);
With static import:
import static org.junit.Assert.assertEquals;
...
assertEquals(expected, actual);
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