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.
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!
Rory visits a beekeeper in Manchester who is gluing wireless chips to his bees.The long wait for a Persian iPhone keyboardPosted on Friday September 22, 2017
The new Apple's iOS has Persian keyboard. BBC Persian's Sam Farzaneh discusses why it is an important feature.Uber London loses licence to operatePosted on Friday September 22, 2017
Ride-hailing app Uber is "not fit and proper" to operate in London, the transport regulator says.Courtesy BBC News