问题
Lets say , there are some import statements in a class. When the byte code is generated for that class, what happens to these import statements.
If the import statements are ignored during runtime, how are the dependencies on that classes methods resolved during runtime.
回答1:
The purpose of import statements is just to make life easier for the human readers (and authors) of the code. Thus they are replaced by references to the fully qualified class/method names in the bytecode. And unused import statements are ignored.
回答2:
import in Java is just a shorthand
so that if you import java.util.*
you don't have to write java.util.ArrayList
in your code but can write ArrayList
回答3:
import
statements are only there for the compiler so it knows what class names (or static method names) you can access unqualified in your code (i.e. MyClass
instead of foo.bar.MyClass
). Behind the scenes this is just used to resolve to the fully-qualified class names which are then used in the bytecode as well.
来源:https://stackoverflow.com/questions/9680295/import-statement-byte-code-significance