A program written by 1.c++ needs to be compiled, called the target file, and then linked before the program is executed before it can be called an executable program that can be run, which can occur before the program is run, or it can occur after the program is run (the process of completing the link in the kernel) is executed. (The run can be understood to be open by double-clicking). Java, in turn, only needs to be compiled into bytecode, and then handed to the JVM parser to execute the link. (The C + + series also has concepts such as dynamic loading and dynamic linking)
2.Java dynamic loading is actually the program in the execution process can be programmed to load the need to be executed bytecode files.
3. Heat substitution is one of dynamic loading, but heat substitution is a special dynamic loading mechanism.
4. Dynamic loading in Java, there are 2 kinds of implementation, the first through Classloader.loadclass () to load, ClassLoader the highest class loader, all classes are owned by him, with loadclass loaded class, is uninitialized, So many static variables and methods cannot be used, and the second method is Class.forName (). The class loaded with this method has already been initialized.
5. And how to achieve heat replacement. First we need to know that the LoadClass in ClassLoader detects whether the class already exists when it loads the class, if it does not, and if it does not, loads the class and caches it. Therefore, the default ClassLoader is unable to achieve hot replacement. In other words, to achieve hot replacement you must implement one's own myclassloader, implement a method of your own to load the class Findclass (), the thing to do in the method is to read the local bytecode file, only the parent class of the DefineClass () method to parse it. This allows the new class to be retrieved, and the old class is reclaimed by GC because the ClassLoader is unreachable.
Here is the code for Myclassloader:
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.nio.ByteBuffer;
Import Java.nio.channels.FileChannel; public class Myclassloader extends ClassLoader {public myclassloader () {} public class<?> Findclass (S
Tring name) throws ClassNotFoundException {String ClassPath = MyClassLoader.class.getResource ("/"). GetPath ();
String fileName = Name.replace (".", "/") + ". Class";
File Classfile = new file (ClassPath, fileName);
if (!classfile.exists ()) {throw new ClassNotFoundException (Classfile.getpath () + "does not exist");
else {bytearrayoutputstream BOS = new Bytearrayoutputstream ();
Bytebuffer BF = bytebuffer.allocate (1024);
FileInputStream FIS = null;
FileChannel FC = null;
try {fis = new FileInputStream (classfile); Fc= Fis.getchannel ();
while (Fc.read (BF) > 0) {bf.flip ();
Bos.write (Bf.array (), 0, Bf.limit ());
Bf.clear ();
} catch (FileNotFoundException var20) {var20.printstacktrace ();
catch (IOException Var21) {var21.printstacktrace ();
Finally {try {fis.close ();
Fc.close ();
catch (IOException var19) {var19.printstacktrace ();
} return This.defineclass (Bos.tobytearray (), 0, Bos.tobytearray (). length); }
}
}