Java Basic Learning----String

Source: Internet
Author: User

Interview questions that often appear in the String class:

Question 1:

String S1 = "abc";
String s2 = "abc";
System.out.println (S1 = = s2);

Result:true

Question 2:

string S1 = new String ("abc");
String s2 = new String ("abc");
System.out.println (S1 = = s2);

Result:false

Question 3:

String S1 = "abc";
String s2 = "a";
String s3 = "BC";
String S4 = s2 + s3;
System.out.println (S1 = = S4);

Result:false

Question 4:

String S1 = "abc";
String s2 = "A" + "BC";
System.out.println (S1 = = s2);

Result:true

Question 5:

String str = "ABC";
Str.substring (3);
Str.concat ("123″");
System.out.println (str);

Result:ABC

For answers to the above questions, you need to synthesize JVM memory allocations and specifications, string source code

First, the Java Virtual Runtime data region:

1. Program counter

2. Java Virtual machine stack

3. Heap

4. Methods area (the method region is logically part of the heap)

5. Run a constant-rate pool (each Run-time constant pool is allocated from the Java Virtual machine's method area)

6, local method stack (similar to Java Virtual Machine stack, main running native method)

The main view here is the run-time constant pool, which is explained in the JVM specification (SE8) as follows:

A Run-time constant pool is a per-class or per-interface run-time representation of the Constant_pool table in a class file (§4.4).

It contains several kinds of constants, ranging from numeric literals known at Compile-time to method and field references That must is resolved at Run-time.

It can be seen that Chang (Constant_pool table) is primarily used to store constants and symbol references generated during compilation, and is stored in the run-time pool of the method area after the class is loaded (Run-time constant pool)

( question 1 and question 2 explained )

For a constant expression, under compiler optimization, the compiler is always evaluated at compile time, and the class is loaded and placed in the run constant pool. (For example "a" + "BC" after compilation, and "ABC" is the same, after loading in the run-time constant pool) ( question 4 in this answer )

There are several constant expressions:

A constant expression is an expression denoting a value of primitive type or a String this does not complete Abru Ptly and is composed using only the following:

literals of primitive type and literals of type String//constant

casts to primitive types and casts to type String//conversion type

the unary operators +,-, ~, and! (but not + + or–)//Monocular operator

the multiplicative operators *,/, and%//correlation operator

the additive operators + and-

the shift operators <<, >>, and >>>

the relational operators <, <=, A;, and >= (but not instanceof)

the equality operators = = and! = (§15.21)
The bitwise and logical operators &, ^, and |

the Conditional-and operator && and the conditional-or operator | |

the ternary conditional operator? :

parenthesized expressions whose contained expression is a constant expression.

simple names that refer to constant variables

qualified names of the form TypeName . Identifier that refer to constant variables

Problem 3 adds two instance objects, and then generates an instance object for the heap, and then reference S4 points to the newly generated instance object, so it returns false;

question 5, view the next string source code to two methods implemented

public String substring (int beginindex, int endIndex) {if (Beginindex < 0) {throw new Stringindexo        Utofboundsexception (Beginindex);        } if (EndIndex > Value.length) {throw new stringindexoutofboundsexception (EndIndex);        } int sublen = Endindex-beginindex;        if (Sublen < 0) {throw new stringindexoutofboundsexception (Sublen); } return ((Beginindex = = 0) && (endIndex = = value.length))?    This:new String (value, Beginindex, Sublen); }
public string concat (String str) {        int otherlen = Str.length ();        if (Otherlen = = 0) {            return this;        }        int len = value.length;        Char buf[] = arrays.copyof (value, Len + otherlen);        Str.getchars (buf, Len);        return new String (buf, True);    }

From the implementation of the two methods, it can be seen that they get information from the Value property of the string object, do not change the value, the string object calls the substring method, it is equivalent to copy a small portion of the content from its own value, similarly, the concat method, A rather string object copies a copy of the same value to others and appends the string to the copied part, returning the result. Both of these methods query the information from the string object, not the change information. So the answer to question 5 is false .

Noted in particular:

The string class has a local method called Intern ()-----Constant Expressions of type String is always ' interned ' so as to share unique instances, USI Ng the method String.intern.

For example:

public class testfinal{public    static void Main (string[] args) {        String a = "abc";        String B = "AB";        String c = "C";        String d = (b+c). Intern ();        String E = (b + "C"). Intern ();        System.out.println (A = = c);        System.out.println (A = = d);        System.out.println (A = = e);}    }

In addition, often asked in the interview, the string class can be inherited, by querying the source of the String class discovery, it is the final class, is not inherited

Refer to the following article:

Deep understanding of Java Virtual Machine ++JVM advanced features and best practices. pdf

Official documentation for Oracle Downloads: "JLS8", "JVMS8"

Some quotes online interviews for a summary of string

Java Basic Learning----String

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.