In core Java, which package is imported by default into every source file without using an explicit import statement?

Difficulty: Easy

Correct Answer: java.lang

Explanation:


Introduction / Context:
Java provides a large standard library organized into packages such as java.lang, java.util, and java.io. Most packages must be imported explicitly with import statements, but one important package is always available without any import. Interviewers ask this question to check your familiarity with core Java packages and to ensure you know which common classes can be used directly by name.


Given Data / Assumptions:

  • We are writing standard Java classes without any special tool or framework.
  • Classes from different packages usually require explicit import statements.
  • Some classes like String and System are available without imports.
  • The Java compiler automatically brings one package into scope by default.


Concept / Approach:
The java.lang package is automatically and implicitly imported into every Java source file. This package contains many fundamental classes, including String, Object, System, Math, Thread, and others that are used extremely frequently. Because these classes are essential for almost any Java program, the language designers decided that programmers should not have to import java.lang manually. Other packages, such as java.util or java.io, still require explicit import statements even though they are commonly used.


Step-by-Step Solution:
Step 1: Recall that classes like String, Object, and System are used in almost every Java program. Step 2: Recognize that you never need to write import java.lang.String; or import java.lang.System; in ordinary code. Step 3: Understand that the reason is that the entire java.lang package is imported automatically by the compiler. Step 4: Check that classes from other packages like java.util.List require explicit imports such as import java.util.List;. Step 5: Conclude that java.lang is the only standard package imported by default into every Java source file.


Verification / Alternative check:
You can verify this by writing a small program that uses String and System without any imports and observing that it compiles correctly. If you try to use a class from java.util, such as List, without an import, the compiler will produce an error unless you fully qualify the class name. The Java Language Specification explicitly states that java.lang is imported automatically, confirming this behavior.


Why Other Options Are Wrong:
Option B is incorrect because java.util, which contains collections, dates, and other utilities, still needs to be imported explicitly. Option C is wrong because java.io, used for input and output streams, is also not imported automatically. Option D is incorrect as java.net, which contains networking classes, always requires explicit imports.


Common Pitfalls:
A common misconception is thinking that other frequently used packages like java.util are also imported by default, which can lead to confusion when compilation errors appear. Another pitfall is overusing fully qualified class names instead of proper imports, which can make code harder to read. Understanding the default import of java.lang helps developers write cleaner, more concise code and correctly manage imports for other packages.


Final Answer:
The package that is imported by default into every Java source file is java.lang.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion