import statement byte code significance

左心房为你撑大大i 提交于 2019-12-08 15:53:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!