问题
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 :
- What do the words "run-time type information" mean? I would be grateful if an example is provided.
- "small fragments of bytecode may be dynamically updated on a running system." According to my understanding, when we use the
javaccommand, Java code is converted into bytecode, and executed by using thejavacommand. So why/how should the fragments of bytecode be updated on a running system?
回答1:
The
compile-time typeof a variable is the type it is declared as, while theruntime typeis the type of the actual object the variable points to. Let's say we have the following:Object obj = new Integer(1);
The
compile-timetype ofoisObject, while its runtime type will beInteger."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:
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.
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) :
- the Eclipselink page on their usage of weaving
- ASM is probably the most popular byte-code manipulation framework.
来源:https://stackoverflow.com/questions/17617899/java-dynamism-clarification