Brief commonplace String Class

String: String class

The most common java class, there are two ways to create a String:

String str = new String(“Hello Word”);
String str = “Hello Word”;

Since the string constant pool optimize phenomenon, java official recommended to use the second way to create a string.
In java String is immutable, the interior of the package and the array of a char final array is modified.

String class from the source can be seen in the final char array being modified

final use

final can be modified classes, methods, variables, classes are not allowed to be modified to inherit, the modified method can only be assigned once again after not modify or assignment, the modified method can not be overridden by subclasses.

String constant pool

String str = new String(“Hello Word”);
String str = “Hello Word”;

Why is this code is still officially recommended that we use the second method to create a String
object?
The reason is because there is a thing called the method area of the java memory structure, the inside has a constant pool will first go to the pool to find the corresponding constant value when creating a String object if you use, if not he will create after the value stored in the constant pool objects, literals in java, constant or literal, constant connection result will be stored in the constant pool.
Its main purpose is to reuse a string, if each time you create an object worthy of the same is a waste of memory
string constant pool optimization
above figure may explain why the recommended String s1 = "abc"; If you use the second way will be java heap memory create a new object to reference objects constant pool

We look at the code

String s1 = “abc”;
String s2 = new String(“abc”);
System.out.println(s1==s2);//false

    String s3 = "abc";
    System.out.println(s3==s1);//true

Because of the constant pool optimize phenomenon we can easily get out of the results, we take a look at the following code

String s1 = “abc”;
String s2 = new String(“abc”).intern();
System.out.println(s1==s2);

This feel that the code is the result? The answer is to true
Intern (): This method has a value corresponding to the constant pool if the constant pool will get from the inside, if no values will be stored into the constant pool and returns its reference, the comparison is based on their equals () method .

Why String is immutable

String is final and so is immutable, then why is it final?

  • For security thread
  • In order to achieve constant pool reuse characteristics
  • For safe operation of the entire program
  • HashCode immutable

1. First String inside the package is modified by the final keyword char array, but this is just a reference to the array address does not change, it also play a role in private, so that in the case of multi-threaded to ensure the security thread .
2. Because String is immutable characteristics in order to achieve constant pool
3. First, open source code can see the String class is final and not allowed to be inherited, it is not through inheritance from the String class method to write inside
4. As characters immutability of strings at the same time create the String class HashCode will be cached, which is why often use HashMap String commonly used as a key reason.

String of common methods

1.charAt (int index): Returns the value at the specified index.
2.indexOf (int ch): Returns the index within a specified character string the first occurrence of
3.length () .: Returns the length of this string
4.startsWith (String prefix): Specifies whether the head position
5.substring ( int beginIndex): string taken
6.toLowerCase (): converts a string to lowercase
7.toCharArray (): returns a char array
8.toUpperCase (): converts a string to upper
9.trim (): removing finishing blank
10.split (string regex): use regular expressions to cut the string
11.lastIndexOf (int ch): returns the index of the last occurrence of
12.endsWith (string suffix): whether a given character ending
13
sign (+) is only a java overloaded operator, at the bottom and is spliced with StringBuilder append method then calls toString () method
a large number of strings together if recommended tringBuffer StringBuilder class or category
of these is I string for understanding, a white came to welcome the god of correction.

Guess you like

Origin blog.csdn.net/weixin_45118251/article/details/90498715