Java.lang package in java

Since the package is java.langautomatically imported into all java programs by the compiler, why do I need to write instructions import java.lang.annotation;at the top of the program when using annotations in the program?

+4
source share
2 answers

Because the package java.lang.annotationdoes not match the package java.lang. These are just different packages.

Imagine that if importing one package imported all packages "under", then

import java.*;

will import almost everything into standard libraries, but that’s not how it works. Import statement

import foo.*;

just imports all types in the package foo- it does not import anything in any other packages that start with foo..

+5

java.lang - , java.lang.

annotation - java.lang, java.lang

+3

Source: https://habr.com/ru/post/1678451/


All Articles