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?

Jon Skeet
people
quotationmark

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..

people

See more on this question at Stackoverflow