java stored procedure

A, java six memory address and explain

1) Register (register): This is the fastest storage area where it is located as different from other storage areas - internal processor. However, the very limited number of registers, the registers allocated by the compiler according to the needs. You can not directly control, we can not feel any signs of the presence of the register in the program.

 

2) the stack (stack): in the general RAM, but can be supported by the processor where it "stack pointer." The stack pointer is moved downward, allocating new memory; if moved upward, to release that memory. This is a fast and effective method for allocating memory, only to register. When creating a program, JAVA compiler must know the exact size and storing all the data in the life cycle of the stack, since it must generate the code to move up and down the stack pointer. This constraint limits the flexibility of the program, although some of it in the stack data storage JAVA - especially object reference, but JAVA objects which are not stored.


3) heap (heap): a versatile memory pool (also present in RAM), for storing all the JAVA object. Benefits of the stack is different from the heap: the compiler does not need to know how much storage area allocated from the heap, do not know the data stored on the heap survive long. Therefore, allocation of storage in the heap there is great flexibility. When you need to create an object, you can just new to write a single line of code that, when executing this line of code will automatically allocate storage on the heap. Of course, this flexibility must be paid for the corresponding code. Storage allocated on the heap than using the stack memory storage needed more time.


4) Static memory (static storage): Here, "static" means "in a fixed position." Stored in static storage program is running has always existed data. You can use the keyword static specific elements to identify an object is static, but JAVA object itself is never stored in static storage space.


5) Constant storage (constant storage): constant value usually stored directly in the program code, it is safe to do so, because they will never be changed. Sometimes, in embedded systems, and other parts of the constant division itself away, so in this case, it may be selected in ROM.


6) Non-RAM memory: If the program data is completely viable outside, it can be any control, the program may not exist in the program is not running.

 

Second, the stack, the stack, the contents stored in the method area

Heap:
1. all stored objects, each object contains information about a class corresponding thereto. (class of object is to obtain an operation instruction)
2.jvm only a heap area (heap) is shared by all threads, the stack does not store the basic types and object references, only the storage object itself.
Stack Area:
1. Each thread contains a stack area, saving only the stack base data type and value data object and the underlying reference
data 2. Each stack (basic data types and object references) are private, other stacks can not be accessed.
3. The stack is divided into three parts: basic types of variables, and the execution environment context, operation instruction area (storage operation instruction).
Methods District:
1. District, also known as static, like the stack, is shared by all threads. The method area contains all the class and static variables.
2. The method area is always included in the only element in the entire program, such as class, static variables.

 

 

Third, the heap (heap) and stack (stack) in the difference in the Java language

1. Stack (Stack) heap (heap) storage areas for all Java Ram the data. Unlike C ++, Java automatically manage the stack and heap, the programmer can not directly set the stack or heap.
2. The advantage of the stack, access speed faster than reactor, located directly behind the CPU registers. But the disadvantage is that there is data in the stack size and survival must be determined, lack of flexibility. Further, the stack data can be shared, see point 3. The advantage is that heap memory size can be allocated dynamically, survival do not have to tell the compiler in advance, Java's garbage collector automatically taken away these data are no longer in use. But the disadvantage is due to the slower dynamic allocation of memory access speed at runtime.
3. Java data types in two ways.
   Base types (primitive types), a total of eight, i.e., int, short, long, byte, float, double, boolean, char ( note that substantially no type of string). This type is defined by such int a = 3; long b = 255L; defined form, referred to as automatic variables. Notably, automatic variables is literal, is not an instance of the class, i.e. the class reference is not, there exists no class. The int a = 3; a is a reference to where the int type, point 3 the literal. These data literal, due to the size found that survival can be seen (these literal definition of a fixed block which, after the block exits, the field value disappears), the pursuit of speed reasons, it is present in the stack .
   In addition, the stack has a very important particularity is that there is data in the stack can be shared. Suppose we also define
   int. 3 = A;
   int. 3 = B;
   The compiler first deal with int a = 3; first it will create a reference to a variable on the stack, and then find there is no literal value of address 3, did not find it to open up a store address 3 of this literal, and then 3 is a point to the address. Followed by treatment int b = 3; b After creating the reference variable, since the stack 3 have the literal, address directly to put b 3. Thus, there have been cases a and b simultaneously point to 3.
   Of particular note is that this literal references cited with a different object class. Assume two classes of objects reference point to an object at the same time, if an object reference variables modify the internal state of the object, another object reference variables immediately reflect this change. Where the value of reference contrast change too, by modifying the value of the literal reference, this does not lead to another point literals. After the above embodiment, we have been defined values of a and b, then make a = 4; then, b is not equal to 4, or equal to three. Inside the compiler encounters a = 4; when it will search again if there is a stack 4 literal, if not, re-open the address stored value 4; if you already have, then this address directly to a point . Thus a change does not affect the value of the value b.
   Another class data packaging, such as Integer, String, Double like corresponding elementary data types of packaged classes. All of these classes of data exists in the heap, Java with new () statement to explicitly tell the compiler, dynamically created at runtime based on need, and therefore more flexible, but the drawback is to take up more time.
4. Each JVM thread has its own private stack space, created with the thread creation, java's stack is stored in different frames, java and c of the stack, just store local variables, return values and call the method, not POP and allows direct push frames, there may be as frames allocated heap, so as j ava stack of allocated memory need not be contiguous. The java heap is shared by all threads, the heap to store all runtime data, which is all object instances and arrays, heap is created when the JVM starts.
   5. String is a special wrapper class data. That you can use String str = new String ( "abc"); to create a form, you can also use String str = "abc"; the form is created (for comparison, before JDK 5.0, you have never seen Integer i = 3;.! expression, and because the class literal is not common except String in JDK 5.0, this is possible because the expression compiler Integer i = new Integer (3) conversion in the background) . The former is a specification of the class creation process, namely in Java, everything is an object, and the object is an instance of the class, all forms by new () to create. Java in some categories, such as DateFormat class, you can return a newly created class by class getInstance () method, appears to violate this principle. actually not. This class using the Singleton pattern returns an instance of the class, but in this instance is to create a class inside by new (), and getInstance () to hide this external details. Why in String str = "abc";, there is no () to create instances through new, is not a violation of these principles? In fact, no.

Fourth, the internal work on the String str = "abc" of. Java statements within this transformed into the following steps:
  (1) to define a reference variable named str String object class: String str;
  (2) checks for any stored value "abc" in the address on the stack If not, open a store literal address value "abc", and then creates a new object o class string, and the string values o address pointing to this, and the next address in the stack this reference note object o. If you already have a value of "abc" address, then find the object o, o and return address.
  (3) the pointing object str o address.
  It is noted that the general class String String values are directly stored value. But like String str = "abc"; in this case, its value string is a reference to a stored data point to the presence of the stack!
To better illustrate this problem, we can be verified by several code.
  Str1 = String "abc";
  String str2 = "abc";
  System.out.println (str1 == str2); // to true
  Note that we are not here by str1. equals (str2); manner, because it will compare the value of the two strings are equal. == Number, according to the instructions of the JDK, and only returns a true value only if two references point to the same object. And we are here to look at is whether str1 and str2 point to the same object.
  Description result, JVM create two references str1 and str2, but only to create an object, and two references point to this object.
  We come back even further, to change the above code:
  String str1 = "abc";
  = Str2 String "abc";
  str1 = "BCD";
  System.out.println (str1 + "," + str2); // BCD, abc
  System.out.println (str1 == str2); // false
  This is He said the assignment changes result in a change in the class object reference, str1 point to another new object! The str2 is still at the original object. The above example, when we changed the value of str1 "bcd", JVM found no storage address of the value on the stack, they opened up this address, and create a new object, which points to the value of the string address.
  In fact, String class is designed as a class immutable (immutable) are. If you want to change its value, you can, but JVM at runtime based on the new value quietly created a new object, and then returns the address of this object to refer to the original class. Although this creation process is completely automatic, but after all, it takes up more time. In time-sensitive environments, with certain adverse effects will be.
  Then modify the original Code:
  String str1 = "ABC";
  String str2 = "ABC";
  str1 = "BCD";
  String Str3 = str1;
  System.out.println (Str3); // BCD
  String str4 = "BCD";
  the System .out.println (str1 == str4); // true
  str3 this object reference directly to the object pointed to by str1 (Note, str3 did not create a new object). When finished str1 change its value, and then create a reference str4 a String, and to point to the new object because str1 modify the value created. It can be found this time str4 does not create new objects, thereby sharing the data in the stack again.
  Let us then look at the following code.
  String str1 = new new String ( "abc");
  String str2 = "abc";
  System.out.println (str1 == str2); // false
  creates two references. It creates two objects. Two references point to two different objects.
  Str1 = String "abc";
  String str2 = new new String ( "abc");
  System.out.println (str1 == str2); // false
  creates two references. It creates two objects. Two references point to two different objects.
  The above described two pieces of code, as long as a new () to create a new object in the heap are created, and its value string is stored separately, even if the same data stack, the data is not shared with the stack .
  (4) the value of the data type of packaging can not be modified. Not just the value of the String class can not be modified, all data types and packaging can not change its value inside.
  (5) Conclusions and recommendations:
  
1) We use such as String str = "abc"; the format when the class definition, always assume that we have created an object str String class. Worried trap! Object may not be created! The only certainty is that references to the String class is created. As for the reference in the end is pointing to a new object, you must consider the context, unless you come dignitaries to create a new object by new () method. Therefore, a more accurate to say that, we create a pointer to an object reference variable str String class, the object reference variables point to a value of "abc" String class. Clearly recognize this bug is difficult to eliminate the program found helpful.
  
2) Use String str = "abc"; the way, can improve the operating speed of the program to some extent, because the JVM automatically according to the actual situation of the stack data to determine whether it is necessary to create new objects. For String str = new String ( "abc "); code, flatly create a new object in the heap, regardless of its string value is equal, whether it is necessary to create new objects, thereby increasing the burden of the program. This idea should be Flyweight pattern of thought, but the internal JDK here to check whether the realization of this model is not known.
  
3) When the comparison value is equal inside packaging, using equals () method; when testing whether two wrapper class reference point to the same object, using ==.
  
4) Since the immutable nature of the String class, String variables when needed constantly changing its value, you should consider using StringBuffer class, to improve program efficiency.
If you can not java heap space allocation success, will throw OutOfMemoryError

 

This article switched: https: //www.cnblogs.com/zyj-bozhou/p/6723863.html

Guess you like

Origin www.cnblogs.com/sxn-learn/p/11310694.html