Categories
Uncategorized

JVM memory allocation and common methods String

A, JVM memory allocation and constant pool

Before introducing the String class, first a simple analysis of the JVM, use of memory is how the allocation. As shown below (note: there is no method jdk1.8 after the region):

As JVM memory is divided into different zones, which have their uses, and create time destroyed, some areas with the virtual machine process exists, some regions are dependent on the thread start and end users to to create and destroy.

Description area names:

1.1, the method area:

Belonging to the shared memory area for storing virtual machine has been loaded class information, constants, static variables, the time compiler to compile the code and other data.

1.2, the virtual machine stack

VM stack is what we usually say that the stack, Java memory model is the method of execution, each execution of a method, you create a stack frame. When the stack frame onto the stack, the return to normal when the result or exception capture Java method invocation stack the stack frame.

Frame stack: the stack frame information storing method, comprising the local variable table number, return values, operand stack, dynamic linking.

1.3, native method stacks

In terms of functionality and virtual machine stack is similar, but the virtual machine stack byte code is executed, and the local stack method is called Native method, and it is thread-exclusive.

1.4, the program counter

The program counter is exclusive thread, which is the line number record bytecodes thread of execution. When multi-threaded execution, CPU will switch back and forth between the threads, then when one thread back again, that is how the thread storage unit and executing instructions. And an address of the program counter will be stored in the storage unit, after the implementation of the program counter is automatically incremented by 1, until it terminates this cycle.

1.5, heap

When it comes to the concept of heap presumably are not familiar, it is an important role in memory. It is mainly used to store object is created, instances out by keyword new, it is one of the largest areas shared by all threads.

== Important Note: JDK1.7 and later, the constant pool to a heap memory. ==

Further comprising a stack == == constant pool for storing generated during compilation == == literal references and symbols. This section after the class is loaded, it is stored in the method area. Meanwhile, new constants generated runtime constant pool may be placed, such as in a class String intern () method produces a constant.

Constant pool is a constant of the type used in the ordered collection. Including direct constant (basic types, String), and references to other types of symbols, methods, fields.

Second, the constant pool

2.1 What is constant:

Constant refers to final modified variable value can not be changed once it is determined.

static final variables can be modified, methods, instance variables and local variables.

The constant pool is divided into two types: static constant pool and runtime constant pool

2.2, static constant pool

* .Class file that is in constant pool, class file constant pool not only contains the string (digital) literal, also contains the class, method of information, most of the space occupied by the file class. This constant pool for storing the literal and symbolic reference amount.

2.3, runtime constant pool

Refers to a virtual machine JVM class loading operations after the completion of the class file constant pool is loaded into memory and stored in the method area, we often say that the constant pool, method of operation is the time constant pool area. A significant amount of pool often run the same feature is dynamic, means that not only will produce constant need at compile time, will save the new constant in the constant pool to run, such as the String class intern () method.

Third, the == and equals

3.1 area between both sides of

==:

For basic types is: a comparative value ==

For reference types is: == shows a comparison of address values

equals:

The comparison is between the two values ​​are equal, but in Java classes are derived directly or indirectly Object class, but also equals no exception. In fact, the source code is the use of equals == comparison, the following source code:

  ![](https://img2018.cnblogs.com/blog/1655301/201909/1655301-20190902223856542-1095893842.png)

So the question is, and this == What difference does it?

Mentioned above equals it is inherited from java.lang.Object, and therefore can be rewritten to define the equals our own way of comparison.

See the following code:

        String str1 = "abc";
        String str2 = "abc";

        char[] strArray = {'a','b','c'};
        String str3 = new String(strArray);

        String str4 = "abc";

        System.out.println(str1 == str2);   
        System.out.println(str1 == str3);   
        System.out.println(str2 == str3);   
        System.out.println(str4.equals(str1)); 

The above operating results as follows:

true
false
false
true

Next, we turn the above analysis results:

1, str1 str2 comparison with the target address is a string, because their values ​​are the same, the address value is also the same.

2, str3 example is the new out of the objects in the heap memory will open up a new memory address, it is not in the constant pool. It returns a value of false.

3, empathy and str2 str3 comparison is the same result.

4, equals the comparison values ​​are identical, the result returned is true.

as the picture shows:

Four, String commonly used method

First statement string:

 String str1 = "abc";

4.1,int length()

 int length = str1.length();
 System.out.println(length);

4.2, char charAt (value)

String str= "abc";
char c = str.charAt(1);  
System.out.println(c); 

4.3,char toCharArray()

String str= "abc";
char c[] = str.toCharArray();  
for (int i = 0; i < c.length; i++) {
      System.out.println("转为数组输出:" + c[i]);
}

4.4, int indexOf ( "character"); int lastIndexOf ( "character")

String str="axcdefgabc";
int a1 = str.indexOf("a"); 
int a2 = str.indexOf("x",  2);
int a3 = str.lastIndexOf("c"); 
System.out.println("你的位置为:" + a1); 
System.out.println("为的位置为:" + a2);
System.out.println("点最后出现的位置为:" + a3);

4.5, string case conversion

toUpperCase (); uppercase
    the toLowerCase (); converted to lower case

String str = "hello world";
String str1 = "HELLO WORD";
System.out.println("将字符串转大写为:" + str.toUpperCase());
System.out.println("将字符串转换成小写为:" + str1.toLowerCase());

4.6, String [] split ( "character")

String str = "abc,def,123";
String[] arr1 = str.split(",");

4.7,boolean equals(Object anObject)

 String str = "abc";
 String str1= "123";  
 if(str.equals(str1)) {  
      System.out.println("相等"); 
 }  
 else{
      System.out.println("不相等");  
 }

4.8,String trim()

String str = "       abc         ";  
System.out.println("去掉左右空格后:" + str.trim());

4.9 string replacement

String replace (char oldChar, char newChar)
    String replaceAll (String, String) will replace all the contents of a specified content
    String repalceFirst (String, String) will be one of the first occurrence of the contents replaced with the content specified

String str = "abcdefgabdc";  
System.out.println("替换:" + str.replace("abc", "123")); 
System.out.println("替换全部:" + str.replaceAll("ab", "12")); 
System.out.println("替换第一次出现:" + str.repalceFirst("a", "a")); 

4.10,String substring(int beginIndex,int endIndex)

 String str = "abcdefg";  
 // 截取0-3个位置的内容, 不含3 
 System.out.println("截取后的字符为:" + str.substring(0, 3));
 // 从第3个位置开始截取, 含2
 System.out.println("截取后字符为:" + str.substring(2));

4.11,boolean equalsIgnoreCase(String)

String str = "ABC";
String str1 = "abc";
if(str.equalsIgnoreCase(str1)){
       System.out.println("相等");
}
else{
       System.out.println("不相等");
}

4.12,boolean contains(String)

String str = "ABCDEF";
String str1 = "ABC";
if(str.contains(str1)){
       System.out.println("str内容中包含ABC");
}
else{
       System.out.println("str内容中不包含ABC");
}

V. Summary

1, to the dispensing JVM memory, there is a method in jdk6 region, there is no method in jdk8 region, into cell regions.

2, there is a method in jdk6 constant pool area, after the move to the stack jdk7 constant pool.

This blog is simply introduces the concept of JVM memory allocation and constant pool, there is still a lot more in-depth principle not recorded. Therefore, the latter will continue to update the knowledge of the principles of the relevant JVM, the above does not apply if the blog also please leave a message (email) advice.

Thanks for reading!

Leave a Reply