Analysis of string of "avoid GetField opcode" and "invariance"

Source: Internet
Author: User

In all programming languages, I think strings should be the most common means of expression on Earth.

In the Java world, string occurs as a class, and the core of a field is a char array, which is internally exported by maintaining an immutable array of char.

This is a string definition of the JDK, first the class is final, indicating that the class cannot be inherited, the core domain is private final, and final indicates that the memory address pointed to by this reference does not change, but it is not sufficient to explain that value[] is immutable Because the value of the memory that the reference points to may change, but the JDK does not let that happen. Private guarantees that the domain is invisible to the outside, and that it is not enough to have a protective copy of value.

To give a simple example:

This is a string constructor, and the parameter is a char array reference, which does not assign the array reference directly to the value member variable of the instance object, but instead copies an array to the object's member variable in a arrays.copyof way. Why is it? Assuming that it is a direct assignment here, then the immutability of the string is completely destroyed, because there is an external reference to the same memory address as the instance object value reference, and the char array object can be changed by an external reference. The end result is that the string is no longer immutable. Fortunately all operations on value in the JDK are protected copy operations, whether assigned or assigned to other external references.

Having said so much, why does Java want a string to remain in an immutable state? The reason is very simple, because Mrs. String is often used by Mrs., there is no more common object on Earth than this one, designed to be immutable, to reduce the overhead of a large number of synchronization locks. Note, however, that a class that is not declared final must be immutable .

As mentioned in the effective Java book, the class is invariant to follow five rules:

1. Does not provide any opportunity to modify the state of the object method

2. Guaranteed class not to be extended

3. All domains are final

4. All domains are private

5. Ensure mutually exclusive access to any mutable component

Interested students can refer to effective 15th, here will not start speaking. Made so long cushion, then can talk about avoid GetField opcode, according to the translation is to prevent "call access to the domain opcode", this tip comes from a piece of comments.

A very common replace method, the internal algorithm is probably such a process: first to find the first oldchar subscript i, copy the contents of the old array before the small mark I to the new array, the new array [i]= ' Newchar ', traversing I after the content, If the old array appears as Oldchar, replace it with Newchar in the new array, or copy the old value to the new array if it does not appear.

At first I was surprised, in the end why, must find the first to appear oldchar subscript, why do not directly traverse the array of each char if the old value, replace with the new value. I consider this algorithm from time complexity and space complexity, and never get the result. I was still too young to realize that I was still trying to maintain a string design principle: "For a case with the same character literal, the string's construction will return the original string object first." This should be done to solve the heap memory.

So, what does this sentence mean? To understand this, you need to have a certain understanding of the JVM.

JVM in the running data area, divided into five parts: Method area, heap area, virtual machine stack, local method stack, program counter.

First, the class-related information must be placed in the method area, the heap put some instance objects, the program counter always points to the next instruction to be executed, the virtual machine stack and the local method stack are used for common methods and local methods.

Focusing on the virtual machine stack, which is thread-private, describes the memory model that is executed by the Java method: Each method creates a stack frame at the same time, for storing local variable tables, operand stacks, method entries, dynamic links, and so on.

A local variable table is used to hold some basic data classes, and references. Operand stacks are used for arithmetic purposes, for example.

int A=1;int b =2;int C =a+b;

The JVM will first put the value of a, a, or a, to the operand stack, and wait until the program counter executes the addition instruction, then A/b pops out of the stack. The point is that a, B value is from where to press into the stack, if not then the next thing to traverse is the value array, value is undoubtedly the data in the heap, that is, every traversal will experience, the data is taken out of the heap, into the stack frame, and then pushed by the stack frame into the stack, the last pop out of the operation

If you execute the above code, the situation is very different, Val is a local variable, it will be stored in the local variable table, the next operation, is the local variable table to the operand stack, belonging to a stack frame data transfer. To avoid frequent stack data transfers, the JVM copies the values to a local variable one time to avoid iterating through each iteration in the next few rows, removing the field values from the heap multiple times.

In addition to what I mentioned, there are many algorithms in string source that are worth learning, design methods, code optimizations, for example, and the implementation of constant pools.

Finally, I want to ask a question. In the JDK7 point version, String introduces a static block of code,

I am puzzled, do not know if there is a great God to help me to read the meaning of this piece of code?

Analysis of string of "avoid GetField opcode" and "invariance"

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.