Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Memory allocation of JVM and Analysis of Common methods of String Class

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "JVM memory allocation and common methods of String class parsing", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "JVM memory allocation and common methods of String class parsing" it!

One, JVM memory allocation and constant pool

Description of the area name:

1.1, method area:

It belongs to the data sharing memory area, which stores the class information, constants, static variables, code compiled by the real-time compiler and other data that have been loaded by the virtual machine.

1.2, virtual machine stack

The virtual machine stack is what we usually call the stack, which is the memory model of the Java execution method. Each time the method is executed, a stack frame is created. Push the stack frame onto the stack, and when the Java method is called, it returns a normal result or catches an exception, the stack frame goes off the stack.

Stack frames: store information about stack frames, including local variable tables, return values, Operand stacks, and dynamic links.

1.3, local method stack

Functionally similar to the virtual machine stack, but the virtual machine stack executes bytecode, while the local method stack invokes the Native method, which is exclusive to threads.

1.4, program counter

The program counter is unique to threads and records the bytecode line number of the current thread execution. During multithreaded execution, CPU switches back and forth between threads, so when you go back to a thread, how do you know the thread's storage unit and execute instructions. The program counter will store the address of the next storage unit, and after execution, the program counter will automatically add 1, so as to cycle until the end of the program.

1.5, heap

When it comes to the concept of heap, it must be familiar, it is an important role in memory. It is mainly used to store the created object, which is the largest area shared by all threads through the keyword new instance.

Special attention: constant pools are moved to heap memory after JDK1.7 and later. = =

The heap also includes a = constant pool, which is used to store the = literals and symbol = = references generated during compilation. This part of the content is stored in the method area after the class is loaded. At the same time, new constants generated by the runtime can also be put into the constant pool, such as those generated by the intern () method in the String class.

A constant pool is an ordered collection of constants used by this type. Includes direct constants (primitive types, String) and symbolic references to other types, methods, and fields.

Second, constant pool

2.1, what is a constant:

A constant is a variable modified by final, and once the value is determined, it cannot be changed.

Final can modify static variables, methods, instance variables, and local variables.

Constant pools are divided into two forms: static constant pools and running constant pools.

2.2, static constant pool

That is, the constant pool in the * .class file. The constant pool in the class file contains not only literal strings (numbers), but also information about classes and methods, which takes up most of the space in the class file. This constant pool is used to store literals and symbolic citations.

2.3, run time Pool

After completing the class loading operation, the JVM virtual machine loads the constant pool in the class file into memory and saves it in the method area. The constant pool we often talk about refers to the running constant pool in the method area. One of the important characteristics of the runtime constant pool is that it is dynamic, which means that constants are not needed only at compile time, and new constants are saved to the constant pool at run time, such as the intern () method in the String class.

Three, = and equals

3.1, the edge between the two

= =:

For basic types: = = represents a comparison of values

For reference types: = = represents a comparison of address values

Equals:

Compare whether the values between the two are equal, but the classes in Java directly or indirectly inherit the Object class, and equals is no exception. In fact, "=" is also used in the equals source code for comparison, as follows:

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

So the question is, what's the difference between this and =?

The equals mentioned above also inherits from java.lang.Object, so we can rewrite equals to define our own way of comparison.

See the following code:

String str1 = "abc"; String str2 = "abc"; char [] strArray = {'aqiang 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 results are as follows:

Truefalsefalsetrue

Next, we analyze the above results in turn:

1 str2 str1 compares the address of a string object with the address of a string object, because their values are the same, so the address values are the same.

2 new str3 is a sample object that opens up a new memory address in heap memory, which is not in the constant pool. So the return result is false.

3. In the same way, the comparison between str2 and str3 is the same.

4Jet equals compares whether the values are the same, so the returned result is true.

As shown in the figure:

Fourth, the common methods of String

First declare the string:

String str1 = "abc"

4.1 pencil int length ()

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

4.2 minute char charAt (value)

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

4.3 Magnechar toCharArray ()

String str= "abc"; char c [] = str.toCharArray (); for (int I = 0; I < c. Output; iSuppli +) {System.out.println ("convert to array output:" + c [I]);}

4.4 indexOf 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 ("your position is:" + A1); System.out.println ("for:" + a2); System.out.println ("the last position of the dot is" + a3)

4.5, string case conversion

ToUpperCase (); convert to uppercase toLowerCase (); convert to lowercase

String str = "hello world"; String str1 = "HELLO WORD"; System.out.println ("convert a string to uppercase:" + str.toUpperCase ()); System.out.println ("convert a string to lowercase:" + str1.toLowerCase ())

4.6 split string [] string ("character")

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

4.7 equals Boolean (Object anObject)

String str = "abc"; String str1= "123"; if (str.equals (str1)) {System.out.println ("equal");} else {System.out.println ("unequal");}

4.8 dint string trim ()

String str = "abc"; System.out.println ("after removing the left and right spaces:" + str.trim ())

4.9, string substitution

String replace (char oldChar,char newChar) String replaceAll (String,String) replaces everything with the specified content

String repalceFirst (String,String) replaces something that appears for the first time with the specified content

String str = "abcdefgabdc"; System.out.println ("replace:" + str.replace ("abc", "123")); System.out.println (" replace all: "+ str.replaceAll (" ab "," 12 ")); System.out.println (" first occurrence of replacement: "+ str.repalceFirst (" a "," a "))

4.10 dint string substring (int beginIndex,int endIndex)

String str = "abcdefg"; / / intercept the content of 0-3 positions, excluding 3 System.out.println ("the intercepted character is:" + str.substring (0,3)); / / intercepted from the third position, including 2 System.out.println ("intercepted character:" + str.substring (2))

4.11 Boolean equalsIgnoreCase (String)

String str = "ABC"; String str1 = "abc"; if (str.equalsIgnoreCase (str1)) {System.out.println ("equal");} else {System.out.println ("unequal");}

4.12 contains Boolean (String)

String str = "ABCDEF"; String str1 = "ABC"; if (str.contains (str1)) {System.out.println ("str content contains ABC");} else {System.out.println ("str content does not contain ABC");}

Fifth, sum up

1. For the allocation of JVM memory, there is a method area in jdk6, so there is no method area in jdk8, which is changed to meta-area.

2the constant pool in jdk7 JDK6 exists in the method area, and moves to the heap after the constant pool.

At this point, I believe you have a deeper understanding of "JVM memory allocation and common methods of String class parsing". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report