Piyush Shrivastava
Piyush Shrivastava

Reputation: 61

java.lang package in java

Since java.lang package is automatically imported in all java programs by compiler, why is it necessary to write import java.lang.annotation; statement at the top of the program while using annotations in the program?

Upvotes: 4

Views: 514

Answers (2)

Leo Bufi Barrameda
Leo Bufi Barrameda

Reputation: 359

Because java.lang automatic import is just for it's classes and interfaces whose package belong it is java.lang.

annotation is a sub-package inside java.lang and this automatic behaviors don't propagate to sub-package only immediate classes of java.lang

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500755

Because the java.lang.annotation package isn't the same as the java.lang package. They're simply different packages.

Imagine if importing one package imported all the packages "under" it - then

import java.*;

would import almost everything in the standard libraries - but that's not the way it works. An import statement of

import foo.*;

simply imports all the types in the foo package - it doesn't import anything in any other packages which happen to start with foo..

Upvotes: 5

Related Questions