The constant pool of knowledge --- [net] base Java_ constant pool of java

Foreword

  Knowledge Network:

  • Constant pool
  • variable
  • constant
  • Object literal      

What is the constant pool

  JVM thread in the constant pool is shared area of memory , Java will  compile the data has been determined  is stored in the constant pool, thus avoiding the impact of frequent create and destroy objects on the system performance.

  The main store of the constant pool is  final constant  value, and the  object literal  values.

  Is not equal to the constant pool data exists , as during compilation, the need to assign a constant whenever it detects whether or not the constant pool back to the value, if the address is directed to the constant value, if not the value, create the value in the constant pool, then worth towards the memory address constants.

Knowledge net

  Java code execution:  We know that, Java program code across platforms will experience two periods: one is compile, and the other is run.

 

To facilitate understanding, we have split again during the second period for the explanation, run. as follows:

  The first stage we called compile this time Java code into class files compiled by javac.
   
  The second we may be called to explain on this point classloader (class loader) class member variable will file, constructor, method and other members loaded into memory.   We will call it the third run and at that point we are familiar with to create objects, open space, allocating memory, execute code.  

 

  Variables: Variables in fact, represents a "workable storage space", it is to determine the spatial location, but what value is placed inside is uncertain. We can access "corresponds to the storage space" by the variable name, so the value of this operation, "storage space" store. 

  Constant: simple understanding is all modified by the final keyword final variables are constants. Constant need to assign at initialization time, and once the assignment can not be changed. That white is a fixed value by the final statement.

  Object literal: We need a new object is usually an object instance of it, and the object literal constant is directly to the object without having to assign new objects out. For example: String objects and packaging part of the basic types of objects. as follows:

  

A 30 = Integer;   // the constant pool. 
B = Integer new new Integer (40);   // open space in the heap memory. 
C 30 = Integer;   // consistent constant pool, and a point address. 

Str1 String = "you";   // in the constant pool. 
String str2 = "good";   // in the constant pool. 
Str3 = String new new String ( "you");   // open space in the heap memory. 
String str4 = "you" + "good";   // "hello" in the constant pool. PS: two determined values, stitching or a fixed value. 
Str5 str1 + = str2 String; // in the heap memory. PS: str1, str2 points during the run value may change, it is not fixed   str4 = str5!   Two people is not a world (not the same memory address) 
String STR6 = str5.intern (); //() Method looks at the intern constant pool in the constant pool if there is a string of equal value, there is a reference is returned, not add the string constant pool. Therefore str4 STR6 ==

 // the PS: Str3 created when, in fact, produce one or both objects and String This constructor relevant public String ( String Original ) {...}  
 // can be seen constructor String It requires a string as parameter. At this new String (...) will create an object in the heap, then the heap memory opens up the corresponding space,
 // find the required parameter string constant pool will go there the same value, there will a direct reference ( this time an object is generated ), the value is not stored in the heap will open memory space, and directs the str3 address space ( case two objects are generated ).
// we can see new String (...) process to open up the space futile, wasted. 

// We know that we usually compare two strings when usually equals () method, which equals () method to compare the "value" is equal, and no more "memory address" is the same.
// == is the comparison of values are equal and the same memory address.

  

Reference: constant pool [base] java in Java_

Guess you like

Origin www.cnblogs.com/HelloHai/p/11754343.html