[Source code] String StringBuffer StringBudlider (1String part), stringbuffer source code
String/** The value is used for character storage. */private final char value [];/** The offset is the first index of the storage that is used. */private final int offset; // The first element/** the count is the number of characters in The String. */private final int count;/** Cache the hash code for the string */private int hash; // Default to 0 hash code/** use serialVersionUID from JDK 1.0.2 for interopera Bility */private static final long serialVersionUID =-6849794470754667710L? You only need to assign a value to the object before it is initialized. You can initialize it in the display initialization-implicit initialization-initialization block initialization-the constructor initializes it in any place, so that the compilation will not report an error when it comes to String, so you have to say one thing, memory graphic memory management Java memory allocation and management is one of the core technologies of Java. We have previously introduced Java memory management and Memory leakage and Java garbage collection knowledge, today, we will go deep into the Java core and introduce Java's knowledge about memory allocation in detail. Java generally involves the following areas in memory allocation: ◆ register: we cannot control it in the program ◆ Stack: stores basic types of data and references of objects, but the object itself is not stored in the stack, but in the heap (new object) ◆ heap: store data generated by new ◆ static domain: static members defined by static in objects ◆ constant pool: storage constant ◆ non-RAM storage: hard disks and other permanent storage spaces are the three most common places for programmers who learn the basics: stack memory heap memory method zone Stack memory computer is used to store non-static variables in the region, Stack is responsible for letting this thing into the Stack when creating non-static variables, when this variable is out of scope, Stack will let it out of the Stack. Static variables are stored in the static area of the method area. Heap memory can open up space allocation objects. The first address of an object is assigned to the reference variable. This is the pointer in Java, which is almost the only place managed by GC! In the method area, there are both methods and static variables. There are several important areas in it: static areas of the Method Area non-static areas constant pool of the Method Area static/non-static areas of the storage is what you can look at my another blog: // If the constant pool of the URL is actually saved as a constant, for example, int a = 10; // the compiler will go to the constant pool to find out if there is 10. If it was used before, it will be used directly, if you have never used it, create 10 in the constant pool, but note that the String object can also be a constant: String st = "123 "; // This is the most special character of String. Note that String is expressed in the following two ways: the most common problem in this area is = equals = the difference between equals and equals = The address value equals is also the address value (more specifically, hashCode), however, this method is rewritten in String to compare the value of the specific content.
Public boolean equals (Object anObject) {if (this = anObject) {return true;} if (anObject instanceof String) {String anotherString = (String) anObject; int n = count; if (n = anotherString. count) {char v1 [] = char v2 [] = anotherString. int I = offset; int j = anotherString. offset; while (n --! = 0) {// compare each character if (v1 [I ++]! = V2 [j ++]) return false;} return true ;}} return false ;}
To learn about String concatenation, you must understand the changes in the memory of these variable constant objects in each step.
String s1 = "hello"; String s2 = "world"; String s3 = "helloworld"; System. out. println (s3 = s1 + s2); // false, s1 + s2 is a new String address reference s3.equals (s1 + s2); System. out. println (s3 = "hello" + "world"); // true, Concatenates the s3.equals ("hello" + "world") found in the constant pool ");
Now, you only need to know that all the things produced by concatenating strings with variables must be new objects. If they are all string constants, you still need to directly query constants in the constant pool. // I keep a special article to introduce what is called the compilation period. If you write it like this:
String a = "AB"; final String bb = "B";/the constant pool String B = "a" + bb; System has been added during compilation. out. println (a = B); // result = true
Method return
String a = "ab"; final String bb = getBB(); String b = "a" + bb; System.out.println((a == b)); //result = false private static String getBB() { return "b"; }
These two things have been confirmed in the compilation phase. This article will help you analyze the update.