Java series (1), java Series

Source: Internet
Author: User

Java series (1), java Series
Overview

This type of article will not update the classic interview questions learned through analysis, so you can easily understand them here. If there is something wrong, please make a brick for the audience.
Today, I will share several frequently-used String questions. I believe that my friends who are learning java are not familiar with the String class. Today, we will come and meet her again. Let's get started.

Let's talk about the features of String.

String is an unchangeable constant. When we create a String object, if this String does not exist in the constant pool of the heap area, A constant pool will be created (where the String is stored, it is called the String pool). If yes, the address of the variable is directly directed to the constant pool. For example: string B = "abc" indicates the memory as follows. Start

1.1
String s1 = new String("abc");String s2 = new String("abc"); System.out.println(s1 == s2);

 

What is the output result?
The above figure also tells us that there are stacks and stack zones in the jvm. In the heap area, local variables are mainly stored, and new objects are stored in the stack area. = The object type is compared with the address. Therefore, s1 and s1 reference the addresses of different new objects in the heap respectively.

The answer is obvious. If the address is different, the output is 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), creates a StringBuffer object, and calls the append () method to return itself. Call the String equals method. The key point is that the equals method has an instance of, which must be of the same type before comparison. Otherwise, false is returned directly.
Let's take a look at the source code:

/*** Compares this string to the specified object. the result is {@ code * true} if and only if the argument is not {@ code null} and is a {@ code * String} object that represents the same sequence of characters this * object. ** @ param anObject * The object to compare this {@ code String} against ** @ return {@ code true} if the given object represents a {@ code String} * equivalent this string, {@ Code false} otherwise ** @ see # compareTo (String) * @ see # define signorecase (String) */public boolean equals (Object anObject) {if (this = anObject) {return true;} // The key point is here. if (anObject instanceof String) {String anotherString = (String) anObject; int n = value. length; 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 will the following code generate in the memory?

String s1 = new String(“abc”); String s2 = new String(“abc”);

 

Answer: 3
With the above analysis, I believe everyone understands that two new objects are added, and a "abc" in the string pool is added ".

1.4

What is the output result of the following code?

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

 

We may not be familiar with the intern () method. Let's take a look at the notes:

/**     * Returns a canonical representation for the string object.     * <p>     * A pool of strings, initially empty, is maintained privately by the     * class <code>String</code>.     * <p>     * When the intern method is invoked, if the pool already contains a     * string equal to this <code>String</code> object as determined by     * the {@link #equals(Object)} method, then the string from the pool is     * returned. Otherwise, this <code>String</code> object is added to the     * pool and a reference to this <code>String</code> object is returned.     * <p>     * It follows that for any two strings <code>s</code> and <code>t</code>,     * <code>s.intern()&nbsp;==&nbsp;t.intern()</code> is <code>true</code>     * if and only if <code>s.equals(t)</code> is <code>true</code>.     * <p>     * All literal strings and string-valued constant expressions are     * interned. String literals are defined in section 3.10.5 of the     * <cite>The Java&trade; 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 this sentence:

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the {@link #equals(Object)} method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. 


That is,If this string does not exist in the constant pool, create one and return the address. Otherwise, return the address directly.

The second line of the above Code is String s2 = new String ("abc"); s2 actually references the new object. Although the intern method is called in the third line, it is not assigned to s2, so the reference of s2 remains unchanged. Therefore, false is returned.
If the code in the third line is changed to s2 = s2.intern (), true is returned.

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

 

Now, we are here today. The analysis will continue later. If you like my articles, please follow me. If you have any questions, please comment. Your support is my greatest motivation !!

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.