Sub topic 7
What `java.lang` Gives You for Free
Prerequisites
This chapter assumes you can write and run a Java class with a main method. You should also know the difference between a class, a method, and a variable, and understand that an import lets you use a shorter class name.
The implicit `java.lang` import
Every Java compilation unit, which is a Java source file, automatically imports all types in the java.lang package. Java behaves as if each file contained this declaration:
import java.lang.*;You do not need to write it yourself. The package contains classes that are central to almost every Java program.
| Simple name | Fully qualified name | Common use |
|---|---|---|
| `String` | `java.lang.String` | Text |
| `System` | `java.lang.System` | Console input and output |
| `Math` | `java.lang.Math` | Mathematical operations |
| `Object` | `java.lang.Object` | Root class of Java objects |
| `Integer` | `java.lang.Integer` | Integer values and conversions |
| `Exception` | `java.lang.Exception` | Recoverable error conditions |
A fully qualified name includes the package name and the class name. For example, the fully qualified name of String is java.lang.String. The simple name is just String.
This program uses several java.lang classes without import declarations:
public class LangDemo {
public static void main(String[] args) {
String message = "Java";
StringBuilder builder = new StringBuilder(message);
builder.append(" packages");
int larger = Math.max(12, 19);
Integer number = Integer.valueOf("42");
System.out.println(builder);
System.out.println("Larger value: " + larger);
System.out.println("Parsed number: " + number);
}
}The compiler resolves String, StringBuilder, Math, Integer, and System as types in java.lang. The program prints:
Java packages
Larger value: 19
Parsed number: 42What is not imported
The automatic import covers java.lang itself, not its subpackages. For example, java.lang.reflect is a different package. Its classes are not available by simple name unless you import them or use a fully qualified name.
The same rule applies to every other package. Java does not automatically import java.util, java.io, or java.time.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println(today);
}
}LocalDate belongs to java.time, so this program needs an explicit import.
Static members still need a qualifier
The implicit import makes types available, not all their static fields and methods. You can write Math.max(3, 7), but you cannot write max(3, 7) unless you add a static import.
import static java.lang.Math.max;
public class StaticDemo {
public static void main(String[] args) {
System.out.println(max(3, 7));
}
}Static imports can shorten expressions, but use them carefully. A qualifier such as Math.max makes the owning class clear.
How the compiler resolves the name
When the compiler sees a simple type name such as String, it searches the names available to that source file. The current package, explicit imports, and the implicit java.lang import all participate in name resolution.
If two available types have the same simple name, use an explicit import for one and the fully qualified name for the other. For example, java.util.Date and java.sql.Date both have the simple name Date.
Exercise
Exercise
Why can a program use String without importing it, but usually needs an import for LocalDate?
Show answer
String is in java.lang, whose types are implicitly imported. LocalDate is in java.time, a separate package, so it needs an explicit import or its fully qualified name.