Which Java packages are imported by default?

For quick answer, three packages are imported by default for each source file. First, the package with no name. Second, the java.lang package. And third, the current package (the package in which the current file is defined).

In every Java program there can be one unnamed package, which is simply a package with no name. If you omit the package statement while writing the class definition, the class name is placed into the default package, which has no name. Java compiler automatically imports this package.

Second, the java.lang package is imported implicitly. Java is a pure object oriented programming language where code is written in form of classes. These class components are called types. Types in Java come in two flavours: built-in or primitive types and types from components. Primitive types can be used directly while component types must usually be ordered from a library by importing them from the appropriate package. The technical term for ordering a component from a library is bringing a component into scope. The Java standard libraries include java.lang package by default, which contains a number of components that are used very commonly in Java programs. Java is useless without much of the functionality in java.lang, that's why java.lang is implicitly imported by the compiler for all programs.

However, one can import the same package or same class multiple times. If you explicitly import java.lang in your program then neither compiler nor JVM complains anything about it, but JVM internally loads the required class only once, no matter how many times you import the same class.

Third, the current package is the one in which the class in execution is defined. Current Java package is automatically imported by Java compiler.

Hope you have enjoyed reading about by default imported packages in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.