Java dynamism clarification

不问归期 提交于 2019-12-10 18:25:26

问题


I came across this excerpt, under the term Java Buzzwords while reading a book on Java which I did not understand.

Dynamic

Java programs carry with them susbtantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe and expedient manner. This is crucial to the robustness of the Java environment, in which small fragments of bytecode may be dynamically updated on a running system.

My questions are :

  1. What do the words "run-time type information" mean? I would be grateful if an example is provided.
  2. "small fragments of bytecode may be dynamically updated on a running system." According to my understanding, when we use the javac command, Java code is converted into bytecode, and executed by using the java command. So why/how should the fragments of bytecode be updated on a running system?

回答1:


  1. The compile-time type of a variable is the type it is declared as, while the runtime type is the type of the actual object the variable points to. Let's say we have the following:

    Object obj = new Integer(1);

    The compile-time type of o is Object, while its runtime type will be Integer.

  2. "small fragments of bytecode may be dynamically updated on a running system."

    This basically means that when debugging some java program, you can make some change and recompile the program and then run it again without the necessity to restart the JVM.




回答2:


  1. The JVM, and Java programs running on the JVM, can get the actual type of an object. In Java, it's impossible to pretend that an object has a given type if it doesn't actually have this type. The JVM will check and detect that, and throw an exception.

  2. When debugging some running code, even remotely, it's possible to modify the source code being run, compile it, and tell the JVM to reload the byte-code without having to stop and restart the program. The Java EE containers, and many frameworks, also generate byte-code at runtime and load it in the running JVM.




回答3:


In addition to what others already said some sources of further information for 2) :

  1. the Eclipselink page on their usage of weaving
  2. ASM is probably the most popular byte-code manipulation framework.


来源:https://stackoverflow.com/questions/17617899/java-dynamism-clarification

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