Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang package in java

Tags:

java

lang

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?

like image 717
Piyush Shrivastava Avatar asked Feb 04 '23 08:02

Piyush Shrivastava


1 Answers

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

like image 62
Jon Skeet Avatar answered Feb 16 '23 18:02

Jon Skeet