关于BootstrapClassLoader、java.lang与java.io的疑问:为何java.io需导入而java.lang无需?
java.lang Doesn't Require Importing, But java.io Does? Great question—this is such a common point of confusion when you're getting to grips with Java's class loading and import rules, so let's break it down in plain terms.
First off, you’re totally correct that the BootstrapClassLoader loads all core classes from rt.jar, which includes every class in both java.lang and java.io. But here’s the key distinction: importing is a compile-time convenience, not a runtime loading requirement.
Let’s unpack the two pieces of this puzzle:
Automatic Default Import for
java.lang
The Java Language Specification (JLS) has a built-in rule: the compiler automatically addsimport java.lang.*;to every single source file you write. You never have to type this line yourself, but it’s there behind the scenes. That’s why you can just writeString s = "hello";instead ofjava.lang.String s = "hello";—the compiler handles translating the short class name to its full qualified path for you.No Default Import for Other Core Packages
Packages likejava.io,java.util, orjava.netdon’t get this special treatment. The compiler has no way of guessing which classes from these packages you want to use, so you have to explicitly tell it via an import statement. If you skip the import, you’ll either get a compiler error, or you’ll have to write the full class name every time (likejava.io.File file = new java.io.File("test.txt");—which gets tedious fast).
A quick clarification to avoid mixing up two separate concepts:
- Class loading (handled by BootstrapClassLoader here) happens at runtime, when the JVM pulls classes into memory as needed.
- Import statements are purely for the compiler—they let you use short, readable class names instead of typing the full package path every time. They don’t change which classes are loaded at runtime.
So even though both java.lang and java.io classes are loaded by the same BootstrapClassLoader, the compiler cuts you a break with java.lang to save you from repetitive boilerplate code.
内容的提问来源于stack exchange,提问作者dreamguy

