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!




Get Free Tutorials by Email

About the Author

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

Today's Tech News

Samsung Galaxy S8 hides home button and gains Bixby AIPosted on Wednesday March 29, 2017

The latest flagship phones from Samsung feature bigger screens and a new kind of virtual assistant.

Samsung Galaxy S8 - but is it art? The internet reactsPosted on Wednesday March 29, 2017

The experts and public respond to the newly unveiled Samsung Galaxy S8 and S8+ smartphones.

Virgin Media overstated superfast broadband rolloutPosted on Wednesday March 29, 2017

Virgin Media suspends four employees after it emerged it overstated its superfast broadband rollout.

Courtesy BBC News