Explore the Java object Model in depth

Source: Internet
Author: User
Tags garbage collection object model

In Java, in addition to the static function or other special description functions, other functions are virtual functions, although there is no virtual keyword, the unified establishment of virtual tables.

Http://www.open-open.com/lib/view/open1382151494993.html

Second, the location of the variables stored in Java

With regard to member methods, Non-static methods have a difference from static methods: A Non-static method has an implied incoming argument, which is given to it by the JVM, and this implied argument is the well-known this pointer (pointing to the current object). The static method does not have this implied parameter and therefore does not require a new object, which can be invoked as long as the class file is loaded by the ClassLoader into the stack of the JVM.

http://blog.csdn.net/yuan22003/article/details/6822221

1. Register: The fastest storage area, by the compiler according to the needs of the allocation, we can not control in the program.
2. Stack: A reference to a variable data and an object that holds the base type, but the object itself is not stored on the stack, but is stored in the heap (the new object) or in a constant pool (the string constant object is stored in a constant pool.) )
3. Heap: Store all new objects.
4. Static domain: Storing static members (defined by static)
5. Constant pool: Holds string constants and base type constants (public static final).
6. Non-RAM storage: Hard disk and other permanent storage space

Here we are primarily concerned with stacks, heaps, and constant pools, which can be shared for objects in stacks and constant pools, and not for objects in the heap. The data size and lifecycle in the stack can be determined, and the data disappears when no reference is directed to the data. The objects in the heap are reclaimed by the garbage collector, so the size and lifecycle do not need to be determined and have great flexibility.
For strings: The references to their objects are stored in the stack, and if the compilation period has been created (defined directly in double quotes), it is stored in a constant pool and stored in the heap if the runtime (new) is established. For equals equal strings, there is always only one copy in the constant pool, with multiple copies in the heap.
such as the following code:

Java code
1.String S1 = "the";
2.String s2 = "the";
3.String s3 = "the";
4.String SS1 = new String ("a");
5.String SS2 = new String ("a");
6.String SS3 = new String ("a");
For the generation of a string through new (assuming "a"), will first go to the constant pool to find out if there is a "China" object, if not, create a string object in the constant pool, and then create a duplicate object in the constant pool for this "China" object. This is the way to interview questions: String s = new string ("XYZ"), resulting in several objects. One or two, if there is no "xyz" in the constant pool, it is two.
For variables and constants of the underlying type: variables and references are stored on the stack, and constants are stored in constant pools.
such as the following code:

Java code
1.int I1 = 9;
2.int i2 = 9;
3.int i3 = 9;
4.public static final int INT1 = 9;
5.public static final int INT2 = 9;
6.public static final int INT3 = 9;

For member variables and local variables: member variables are external to the method, internally defined variables of the class, and local variables are variables defined within the method or statement block. Local variables must be initialized.
The formal parameter is a local variable, and the data of the local variable exists in the stack memory. The local variables in the stack memory disappear as the method disappears.
The member variable is stored in the object in the heap and is reclaimed by the garbage collector.
such as the following code:

Java code
1.class Birthdate {
2. private int day;
3. private int month;
4. private int year;
5. Public birthdate (int d, int m, int y) {
6. Day = D;
7. Month = m;
8. Year = y;
9.}
10. Omit the Get,set method ...
11.}
12.
13.public class test{
public static void Main (String args[]) {
15.int date = 9;
Test test = new test ();
Test.change (date);
Birthdate d1= New Birthdate (7,7,1970);
19.}
20.
public void Change1 (int i) {
i = 1234;
23.}
For the above code, date is a local variable, i,d,m,y is a local variable, and Day,month,year is a member variable. The following is an analysis of the changes in code execution:
1. The main method begins execution: int date = 9;
Date local variables, underlying types, references, and values all exist in the stack.
2. Test test = new test ();
Test is a reference to an object, where the object (new Test ()) exists in the heap.
3. Test.change (date);
I is a local variable, and references and values exist in the stack. When the method change execution completes, I will disappear from the stack.
4. Birthdate d1= new Birthdate (7,7,1970);
D1 is a reference to an object, where the object (new Birthdate ()) exists in the heap, where D,m,y is stored on the stack for local variables, and their types are underlying types, so their data is also stored on the stack. Day,month,year are member variables that are stored in the heap (new Birthdate ()). When the birthdate construction method is finished, the d,m,y disappears from the stack.
After the 5.main method finishes, the date variable, the TEST,D1 reference disappears from the stack, new test (), and the new birthdate () waits for garbage collection.


Three, JAVA's constructor

The contents of the link below are very good and must be carefully savored.

Http://wenku.baidu.com/link?url=xe_nOJsPd15ntVqFq5kHZ3nLHpW1b58RNnNtgkTa2lE74s-2s774JkBU8FBjD9hCR_ Nya6cdrnzyptwu1zuhm5uzmomg2f6bwyta9vlf1mw






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.