Java Programming Ideas (16)--Contact the JVM and talk about class

Source: Internet
Author: User

Programming thought this column stopped for a long time, mainly focused on other knowledge, and now continue to fill.


The first two articles are written to Rtti and a brief reflection of the introduction, a look back:

RTTI, runtime type information, polymorphic applications, type conversions are actually occurring during run time.


Class object:

In the definition of programming, Java uses Class objects to perform its rtti, classes are part of a program, each class has a class object, and each time a new class is written and compiled, a class object is created, which is actually saved in the. class file of the same name. The generation of this class object is actually a subsystem of the JVM (the Java Virtual machine) that uses the ClassLoader.


Add the definition of Baidu:

in Java, each class has a corresponding class object. That is, when we write a class, after the compilation is complete, in the generated. class file, a class object is generated that represents the type information for this class. The last sentence is the point.


While class is a flag for a class name, Class A,a is a class, and class is an object.


is not very vague, these concepts, where class pops up, in fact there is a classloader. In order to understand the relationship, this is definitely a virtual machine in addition to the "in-depth understanding of the JVM," specific to ask Rednaxelafx elder brother what books are better. He recommended "Inside the Java Virtual machine". After reading five chapters, there is a general direction, this book also recommend others, not too difficult to understand, in my sophomore time to see a red cover of the JVM book to see the real pain, one is the code directly to the assembly, so that the right time to see the right book is very important.



"Inside the Java Virtual machine" a book Note:

The first 4 technologies for the entire architecture are grasped:

languageclass fileAPIJvm

The book is a bit of a reasonable analysis, so-called cross-platform, is because the class file, binary files, but like C + + C These, compiled and linked binary files are directly running in the system, for the target processor's machine language
is not cross-platform, and the class file is the "machine language" of the virtual machine.

And in order to run everywhere, the Java graphical interface API is more complex, why AWT and swing is not widely used, the individual think that one is the need for the corresponding Java environment, one is the development efficiency problem.

There is also a very interesting point: Java files running in the DOS window, Java HelloWorld, is not always think of what Java means, in fact, Java is called the system run the JVM, loaded HelloWorld after the main method of running.


Now look at the JVM's architecture diagram:




The things in this diagram can be linked to the Java programming Idea (a)-Everything is object and memory allocation.

The method area and heap for each virtual machine instance are shared by all threads inside the instance, and the heap holds the object.
The creation of a new thread will have its own PC register (program counter) and a Java stack (the intermediate result of saving the thread's local variable parameter return value operation).
The Java stack is made up of stack frame stack frames or frame frames, and the thread calls a method that presses a new stack frame and pops up when it returns.

To the point of learning, for each loaded type, the virtual machine will create an instance of class for it,
A reference to the class object that represents Object Class.forName (Java.lang.Object).
If the requested type cannot be reproduced into the current namespace, forname throws ClassNotFoundException.

Another way, GetClass can also get the class object. There are getname and getClassLoader methods in the Class object.
Directly on the example:
public class TestClass {public static void main (string[] args) {Class C = Object.class.getClass (); Class C1 = object.class;object o = new Object (); Class OC = O.getclass (); Class ct = null;try {ct = class.forname ("Java.lang.Object");} catch (ClassNotFoundException e) {//TODO auto-generated Cat CH Blocke.printstacktrace ();} TestClass t = new TestClass (); Class Tclass = T.getclass (); System.out.println (Tclass.getclassloader ()); System.out.println (C.getclassloader ()); System.out.println (Oc.getclassloader ()); System.out.println (Ct.getclassloader ()); System.out.println (C1.getclassloader ()); System.out.println (Tclass.getname ()); System.out.println (C.getname ()); System.out.println (Oc.getname ()); System.out.println (Ct.getname ()); System.out.println (C1.getname ());}} Output:[email protected] NullnullnullnullTestClassjava.lang.Classjava.lang.Objectjava.lang.Objectjava.lang.Object

Two methods return the information of the loaded type, GetName get the fully qualified name, the class belongs to the package + class name, if it is the object class, is actually java.lang.object.
Object.class refers to the class object reference, loaded as object, and then GetClass, the load becomes a class reference, so GetName got the class, and Object.class.getName () is loaded with object, so GetName gets the fully qualified name of object.

There are two types of class Loaders: The Startup class and the custom class, which are defined by the JVM and are written by yourself later.
getClassLoader returns the ClassLoader reference, or null if the class loader is started.



the difference between new and class.newinstace.
is the creation of an instance, what is the specific difference?
Referenced by: Http://www.coderanch.com/t/516460/java/java/newInstance-difference-bet
T. Huy Nguyen said.
An obvious difference is, the class ' s fully qualified name can be dynamically constructed/read from file/changed when You use "newinstance".

"Newinstance" also requires the Java class to a No-arg constructor, else you'll get error at runtime. If you do the same with "new" and you'll immediately get error at compile time.

The obvious difference is that when you use Newinstance, the name of class can dynamically be constructed/read dynamically from File/change.
Newinstance requires a Java class to have a parameterless constructor, or run-time errors will occur. In the same case, in new, an error occurred during compilation.

Summarize:
For each loaded type, the virtual machine creates an instance of the class for it accordingly.
The virtual machine stores type information in the method area:
The fully qualified name of the type.
The fully qualified name of the direct superclass.
Type is an interface type or a class type.
The access modifier.

And these things, in the programming time how to get, rely on class, then you will ask, get what?
The use is reflection, by a class name, I can get a lot of information, but also can manipulate it, this is the role of the.

Java Programming Ideas (16)--Contact the JVM and talk about class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.