Overview
This type of article will not be updated analysis of the classic interview topics, recorded in this easy to understand. If there is a wrong place also ask the audience to shoot bricks.
Today the main share of the common string of several topics, I believe that the learning of Java's small partners on the string class is more familiar with, today we come and she met again, OK below the beginning.
First, the string feature.
String is an immutable constant, and whenever we create a string object, if the string does not exist in the constant pool of the heap, it creates a store in the constant dominant (called String pool where the string is stored) and, if it exists, directly points the address of the variable to the constant pool. For example: String b = "ABC" This sentence memory is indicated as follows. Here's the first question.
1.1
String s1 = new String("abc");String s2 = new String("abc"); System.out.println(s1 == s2);
What is the result of the output?
The above figure also probably says that there are heaps and stacks in the JVM. In the heap area, the main storage is the local variables, the stack area is stored in the new object. = = The address is compared for the object type. So in S1 and S1 are the addresses of the different objects that are referenced in the heap, respectively, and the graph is understood as follows
The answer is obvious, the address is different output false.
1.2
String s1 = "abc";StringBuffer s2 = new StringBuffer(s1); System.out.println(s1.equals(s2));
Is this true or false? The answer is false.
First the S1 variable references the string "abc", then StringBuffer s2 = new StringBuffer (S1), and a new StringBuffer object called the Append () method returns itself. Call the Equals method of string. The point is that this equals method has a instance of, must be the same type to be compared otherwise directly return false.
Take a look at the source code:
/** * ComparesThis string to the specified object. The resultis {@code *TrueIfand onlyIf the argumentIsnot {@codeNullandis a {@code * String} object that represents the same sequenceof characters asThis * object. * *@param anobject * The object to compareThis {@code String} against * *@return {@codeTrueIf the given object represents a {@code String} * equivalent toThis string, {@codeFALSE} otherwise * *@see#compareTo (String) *@see#equalsIgnoreCase (String) */public Boolean equals (Object anobject) {if (this= = anobject) { return true;} //Key point is here if (anobject instanceof String) {string anotherstring = (string) anobject; int n = value.length; C7>if (n = = anotherString.value.length) {char v1[] = value; char v2[] = anotherstring.value; int i = 0; while (n--! = 0) { if (v1[i]! = V2[i]) return false; i++;} return true;}} return false;}
1.3
How many objects does the following code produce in memory?
string S1 = new String ("abc");
String s2 = new String ("abc");
Answer: 3
With the above analysis, I believe we all understand that new two objects, plus a string pool of "abc".
1.4
What is the result of the following code output?
String s1 = "abc";String s2 = new String("abc");s2.intern();System.out.println(s1 ==s2);
We may not be familiar with the intern () approach, let's take a look at the notes:
/** * Returns a canonical representationForTheString object. * <p> * A Poolof strings, initially empty,is maintained privatelyByThe *Class <code>string</code>. * <p> * WhenThe Intern methodis invoked,IfThe pool alreadyContains a *StringEqualTo this <code>String</code> objectAs determinedby *The {@link#equals (Object)} method, then the string from the pool is * returned. Otherwise, this <code>String</code> objectis addedToThe * poolandA referenceTo this <code>String</code> objectis returned. * <p> * It followsThatFor any strings <code>s</code>and <code>t</code>, * <code>s.intern () == t.intern () </code>Is <code>True</code> *Ifand onlyIf <code>s.Equals (t) </code>Is <code>True</code>. * <p> * all literal stringsandstring-valued constant expressions is * interned. String literals is defined in section 3.10.5 of the * <cite>the Java™ Language Specification</cite>. * * @return a string that has the same contents as this string, but is * Guaranteed to be from a pool of unique strings. */Public native String intern ();
Note A lot of my grass, the key is the sentence: When the Intern method is invoked, if the pool already contains a string equal to this String object as deter Mined by
The {@link #equals (Object)} method, then the string from the pool is
Returned. Otherwise, this String object was added to the
Pool and a reference to this String object is returned.
Basically, if the string does not exist in the constant pool, it creates one and returns the address, otherwise it returns the address directly.
The preceding code is the second line of string s2 = new String ("abc"); S2 actually refers to the object of new, although the Intern method is called in the third row, but is not assigned to S2, so the S2 reference remains unchanged. So return false.
If the third line of code is changed to S2 = S2.intern (), it returns true.
String s1 = "abc"; String s2 = new String("abc"); s2 = s2.intern(); System.out.println(s1==s2);
All right, here we go today. The analysis will continue afterwards. If you like my article, please follow me. All of you crossing's support is my greatest motivation!!
Java you may not know about the series 1