✏️ Explanatory Question

[Packages]

Question:

Why is the lang package termed as the default package in Java?

👁 0 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

Answer:

The java.lang package is called the default package because it is automatically imported into every Java program by the Java compiler.

Explanation:

Normally, packages must be imported before using their classes.

Example:

import java.util.Scanner;

But classes from java.lang can be used directly without writing any import statement.

Examples of classes in java.lang:

  • String
  • System
  • Math
  • Integer
  • Character

Example:

String s = "Java";

System.out.println(Math.sqrt(25));

Here, no import statement is needed because these classes belong to:

java.lang

Important Concept:

  • java.lang is imported automatically by default
  • Therefore, it is called the default package

Conclusion:

Since the Java compiler automatically imports the java.lang package in every program, it is known as the default package.