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
?
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);