class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In the above example we are using the println
method without importing the package of it. So I want to know: What are the packages or classes included automatically?
Everything in java.lang
is imported by default - here you are using java.lang.System
and java.lang.String
There are two packages imported by default:
java.lang
From the language spec:
Code in a compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang.
So, you only have access to public types in java.lang
, but you have access to all top-level types in the current package.
But it's important to note that Java packages aren't hierarchical, despite the appearance, so this means that e.g. java.lang.reflect
is not also imported automatically because of java.lang
being imported.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With