Why don't you need to import String, Integer, or Object, and what makes the java.lang package special?
answer
- Every file implicitly does `import java.lang.*;`
- java.lang = Object, String, wrappers, System, exceptions
- It's wildcard-strength → can be shadowed by same-package/single import
- Sub-packages (java.lang.reflect) are NOT auto-imported
- Don't redundantly import java.lang types
basics
~10 sEvery Java file automatically imports the java.lang package, where String, Integer, Object, System, and similar core types live. So you can use them by simple name without writing any import.
solid answer
~40 sThe Java compiler implicitly performs `import java.lang.*;` in every compilation unit. `java.lang` holds the foundational types the language itself depends on — `Object`, `String`, the primitive wrappers (`Integer`, `Boolean`…), `System`, `Math`, `Thread`, `Throwable`/`Exception`, and so on — so they're always available by simple name with no explicit import. This is the only package auto-imported (besides types in your own package being visible). It's a wildcard-strength import, so it can be **shadowed**: if you declare your own class named `String` in the current package (or import one by name), your `String` wins over `java.lang.String` because same-package/single-type imports out-rank the implicit on-demand `java.lang.*`. Sub-packages like `java.lang.reflect` are NOT auto-imported — only `java.lang` itself.
go deeper
Knows core types like String/Object/System need no import because java.lang is implicitly available.
Explains it's an implicit java.lang.* wildcard, lists representative members, and knows sub-packages aren't auto-imported.
Adds that the implicit import is wildcard-strength and thus shadowable by same-package/single-type imports, with the String-shadowing example.
Connects the rule to naming conventions and code-review standards (never shadow core types), and explains why the language must keep these types always in scope (literals, autoboxing, exception model).
## The implicit import Every Java source file behaves as if it begins with: ```java import java.lang.*; ``` This is added automatically by the compiler. That's why you can write `String s = "hi";` or `System.out.println(...)` or `new Object()` with no import line — those types live in **`java.lang`**. ## What's in java.lang `java.lang` is the package of types so fundamental that the language can't function without them: - `Object` — the root of every class hierarchy. - `String` — string literals are `String` instances. - Primitive **wrapper** classes: `Integer`, `Long`, `Double`, `Boolean`, `Character`, etc. (autoboxing depends on these). - `System`, `Math`, `Thread`, `Runnable`. - The throwable hierarchy: `Throwable`, `Exception`, `RuntimeException`, `Error`. - `Class`, `Enum`, `Comparable`, `Iterable`, `CharSequence`. Because the language semantics (string literals, exceptions, the class hierarchy, autoboxing) reference these directly, they must always be in scope. ## It's a wildcard — so it can be shadowed The implicit `java.lang.*` is an **on-demand (wildcard)** import, which is the *weakest* form in the precedence ladder. Therefore: - A type in your **own package** with the same simple name shadows it. - A **single-type import** with the same simple name shadows it. Example (a terrible idea, but legal): ```java package com.example; class String { } // your own String class Use { String s; // refers to com.example.String, NOT java.lang.String! } ``` Here your `String` (same package) out-ranks the implicit `java.lang.String`. This is why shadowing core names is strongly discouraged. ## Only java.lang itself — not its sub-packages Auto-import covers exactly the `java.lang` package. **Sub-packages are separate packages and are NOT auto-imported:** - `java.lang.reflect.Method` → needs `import java.lang.reflect.Method;` - `java.lang.annotation.Retention` → needs an explicit import. This mirrors the general rule that wildcard imports never recurse into sub-packages. ## Practical takeaways 1. Never import `java.lang` types explicitly — it's redundant (some linters flag it). 2. Don't name your own classes after `java.lang` types; you'll create confusing shadowing. 3. Remember `java.lang.reflect`, `java.lang.annotation`, etc. **do** need imports.
- Do you need to import java.lang.reflect.Method?Yes. Only the `java.lang` package itself is auto-imported; sub-packages like `java.lang.reflect` are separate packages and require an explicit import.
- What happens if you declare a class named String in your own package?Your `String` shadows `java.lang.String` within that package, because same-package types out-rank the implicit wildcard `java.lang.*`. It compiles but is extremely confusing and should be avoided.
saying these in an interview costs you the question
- Thinking you must import String/Object/System
- Believing java.lang.reflect is auto-imported too
- Claiming the implicit import can't be shadowed
- Saying every java.* package is auto-imported