Java String in-depth understanding

Source: Internet
Author: User
Tags ming stringbuffer

    • Say the output of the following program
classStringequaltest { Public Static voidMain (string[] args) {String S1 = "Programming";
String s2 = new String ("Programming");
String s3 = "program" + "Ming";
String S4=new string (s2);
String S5 = "Programming";
System.out.println (S1 = = s2);//false
System.out.println (S4 = = s2);//false
System.out.println (S1 = = S3);//true
System.out.println (S1 = = S1.intern ());//true}}

Explain:

The concept of constant pooling is introduced here:
Chang (Constantpool) refers to some of the data that is determined at compile time and is saved in the compiled. class file. It includes constants in classes, methods, interfaces, and so on, including string constants

First, we need to know that Java ensures that a string constant has only one copy.  

Because the example in S1 and S3 in the
Programming”都是字Character string constants, which are determined at compile time, so S1==S3 is true, and "program" and "Ming" are also string constants, and when a string is concatenated by multiple string constants, it is itself a string constant. So S3 is also parsed as a string constant at compile time, so S3 is also a reference to "Kvill" in the constant pool.

So we come to s5==s1==s2;

Strings created with new string () are not constants and cannot be determined at compile time, so the strings created by new string () are not placed in the constant pool, they have their own address space.

So there's s2!. =s4

The Intern method of the string object will get a reference to the version of the strings object in the constant pool (if there is a string in the constant pool and the equals result of the string object is true), if there is no corresponding string in the constant pool, the string is added to the constant pool. It then returns a reference to the string in the constant pool.

Another point: the constant pool that exists in the. class file is loaded by the JVM at runtime and can be expanded. The Intern () method of string is a method of extending a constant pool, and when a string instance str calls the Intern () method, Java looks for a constant pool with the same Unicode string constant, if any, and returns its reference, if not, Adds a string in the constant pool that is Unicode equals STR and returns its reference;

案例

String s0="Kvill"; String S1=NewString ("Kvill"); String S2=NewString ("Kvill"); System.out.println (S0==S1); System.out.println ("**********"); S1.intern (); S2=s2.intern ();//assign a reference to "Kvill" in the constant pool to S2System.out.println (s0==S1); System.out.println (S0==S1.intern ()); System.out.println (S0==S2); The result is:false**********false //although S1.intern () is executed, its return value is not assigned to S1true //Description S1.intern () returns a reference to "Kvill" in a constant pooltrue

    • 关于equals()和==:

This is simple for string to compare whether the Unicode sequence of the two strings is equivalent, and if equal returns true; The string class has overridden the Equals method.

and = = is the same address that compares two strings, That is, whether it is a reference to the same string .

In the conforming data type, equals and = = are compared between the two objects, unless overridden by equals, see Hashcode and equals.

    • Compare two identical StringBuffer objects using the Equals method
StringBuffer sb0=New stringbuffer ("Hello");        StringBuffer sb1=new stringbuffer ("Hello");        System.out.println (sb0= =sb1);        System.out.println (Sb0.equals (SB1));        System.out.println (Sb0.tostring (). Equals (Sb1.tostring ())); false false true

The StringBuffer class does not override the Equals method under the object class, so the actual or = = comparison, so if you need to compare the equality of two StringBuffer objects, the ToString () method is converted to string.

  • . About string is immutable

    This said again a lot, as long as we know that the string instance once generated will not be changed, such as: string Str= "kv" + "ill" + "+" ans ";
    Is that there are 4 string constants, first "KV" and "ill" generated "Kvill" in memory, and then "Kvill" and "" Generate "Kvill" in memory, and finally generated a "Kvillans", and the address of the string is assigned to STR, This is because the "immutable" of string produces a lot of temporary variables, which is why it is recommended to use StringBuffer, because StringBuffer can be changed.
  • Is it possible to use multiple delimiters in a split string

String str= "2012-05-25 13:24:16";        String[] s=str.split ("-|:| \\s ");          for (String temp:s) {            System.out.print (temp+ "");        } 2012 05 25 13 24 16

The key point is to use multiple separators in the split () method, in fact the split () method is a regular expression, so you can use the OR operator (|) to link multiple delimiters, so that when split, the string will be divided by all the delimiters. \s represents a space, but you also need to add the escape character ' \ ' before \s, otherwise a compilation error will occur.

Java String in-depth understanding

Related Article

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.