Java basics-String class (1), java basics string class

Source: Internet
Author: User

Java basics-String class (1), java basics string class

I. StringClass represents a string

All character strings in Java (such"abc".

Strings are constants, and their values cannot be changed after creation. The string buffer supports variable strings. Because the String object is unchangeable, it can be shared. For example:

1 String str = "abc ";

It is equivalent:

1 char data [] = {'A', 'B', 'C'}; 2 String str = new String (data );

The following provides more examples of how to use strings:

1 System.out.println("abc");2      String cde = "cde";3      System.out.println("abc" + cde);4      String c = "abc".substring(2,3);5      String d = cde.substring(1, 2);

StringClasses can be used to check a single character, compare string, search string, extract substring, create a copy of string, and convert all characters to uppercase or lowercase letters. The case-sensitive ing is based on the Unicode Standard Edition specified by the Character class.

The Java language provides special support for String concatenation symbols ("+") and conversion of other objects into strings. String concatenation is throughStringBuilder(OrStringBuffer) Class and itsappendMethod. String Conversion is throughtoStringMethod.ObjectClass definition, and can be inherited by all classes in Java. For more information about String concatenation and conversion, see The Java Language Specification co-authored by Gosling, Joy, and Steele.

Unless otherwise statedNullThe constructor or method passed to this class will throw a null pointer exception.

StringRepresents a string in UTF-16 format, with supplementary characters represented by a proxy pair. Index value referscharCode unit, so add characters inStringWhich occupies two locations.

StringClass provides Unicode code points (characters) and Unicode code units (I .e.,charValue.

Ii. How to Create strings

1 String s = "abc ";

Creates a reference s on the stack. It will first check whether there is a constant "abc" in the constant pool. If so, points s to "abc" in the constant pool ".

If no, create abc in the constant pool,

1 String s = new String ("abc ");

Equivalent to String obj = "abc"; String s = new String (obj); after this operation, there are two data copies in the memory: One copy in the constant pool and one copy in the heap. With the new operation, no matter whether the constant pool originally has "abc", it will create a copy on the heap.

3. String comparison

Example 1: Use of String constant pool

1 String s0 = "abc"; 2 String s1 = "abc"; 3 System. out. println (s0 = s1); // true 4 // both s0 and s1 point to the same "abc" in the constant pool"

Example 2: difference between = and equals in String

1 String s0 = new String ("abc"); // The new operation will generate an object on the heap. s0 points to heap 2 String s1 = new String ("abc "); 3 System. out. println (s0 = s1); // false s0 and s1 point to the objects of different de on the stack: System. out. println (s0.equals (s1); // true because the String class overrides the equals method, which is equivalent to the object content.

Example 3: confirmation during compilation

1 String s0 = "helloworld"; 2 String s1 = "helloworld"; 3 String s2 = "hello" + "world"; // when compiling, directly compiled into helloworld4 System. out. println (s0 = s1); // true5 System. out. println (s0 = s2); // true

Example 4: The compilation period cannot be determined

1 String s0 = "helloworld"; 2 String s1 = new String ("helloworld"); 3 String s2 = "hello" + new String ("world"); 4 System. out. println (s0 = s1); // false indicates a constant pool and a heap System. out. println (s0 = s2); // false5 System. out. println (s1 = s2); // false

Example 5: compile-time optimization

 1 String s0 = "a1"; 2 String s1 = "a" + 1; 3 System.out.println((s0 == s1)); //true 4                    5 String s2 = "atrue"; 6 String s3= "a" + "true"; 7 System.out.println((s2 == s3))  //true 8                    9 String s4 = "a3.4";10 String s5 = "a" + 3.4;11 System.out.println((s4 == s5));  //true

Example 6: The compilation period cannot be determined

1 String s0 = "AB"; 2 String s1 = "B"; 3 String s2 = "a" + s1; // s1 is not a constant and cannot be determined during compilation 4 System. out. println (s0 = s2); // false

Example 7: confirmation during compilation

1 String s0 = "AB"; 2 final String s1 = "B"; // when final is added, it becomes a constant 3 String s2 = "a" + s1; // for the addition of two constants, the compiler can determine its value 4 System. out. println (s0 = s2); // true

Iv. String object memory Analysis

// Example 1

String a = "abc"; ①

String B = "abc"; ②

Analysis:

① After code execution, a String object with the value of abc is created in the constant pool,

② During execution, no new String object is created because "abc" exists in the constant pool.

// Example 2

String c = new String ("xyz"); ①

String d = new String ("xyz"); ②

Analysis:

① When the Class is loaded, "xyz" is read as a constant. A String object with the shared value "xyz" is created in the constant pool;

Then, when new String ("xyz") is called, the new String ("xyz") object is created in heap;

② Because "xyz" exists in the constant pool (constant pool), "xyz" is no longer created, and a new String ("xyz") is created ").

// Example 3

String s1 = new String ("xyz"); // create two objects (in constant pool and heap), one reference

String s2 = new String ("xyz"); // creates an object (in the heap), and creates an object each time it is executed, a reference

String s3 = "abc"; // create an object (constant pool) and a reference

String s4 = "abc"; // do not create an object (share data in the previous constant pool), just create a new reference s4)

// Example 4

1 public static void main (String [] args) {2 // The following two statements create an object. "Fengshan" is stored in the String constant pool 3 String str1 = "fengshan"; 4 String str2 = "fengshan"; 5 System. out. println (str1 = str2); // true 6 // The following two statements create three objects. "Tianye" is stored in the String constant pool, and two new String () objects are stored in heap memory 7 String str3 = new String ("Tianye "); 8 String str4 = new String ("Tian e"); 9 System. out. println (str3 = str4); // false 10 // The following two statements create an object. 9 is an object stored in the stack memory // It refers to 9, and I and j are references to 9 11 int I = 9; 12 int j = 9; 13 System. out. println (I = j); // true 14 // because no packing is available, the following two statements create two objects. Two 1 objects are stored in the heap memory with 15 Integer l1 = new Integer (1). Note that there is no packing operation for 16 Integer k1 = new Integer (1); 17 System. out. println (l1 = k1); // false 18 // The following two statements create an object. 1. The object is stored in the stack memory. When auto-packing is performed, an instance is used for values ranging from 127. 19 Integer l = 20; // boxed 20 Integer k = 20; // boxed 21 System. out. println (l = k); // true 22 Integer i1 = 256; // The following two statements create two objects. I1, i2 variables are stored in the stack memory, and two 256 objects are stored in the heap memory 23 Integer i2 = 256; 24 System. out. println (i1 = i2); // false 25}

5. common String operations

Common Operations on strings include obtaining, judging, conversion, replacement, and cutting.

1) Get class operations

String str = "When is the Spring Festival and autumn? What is the past? The building turned east again last night, and the motherland was overwhelmed by looking back at the moon and tomorrow ";

1. How long is this string?

2. What are the 4th characters that are used to obtain Characters Based on the index?

3. The first comma is the character number, that is, the index (take the character (or string) position based on the character)

4. Index of the last month

5. Whether the string contains the "moon"

6. Is it starting with "Spring Flower" or ending with "mm-Ming "?

7. Is this string empty?

8. Is it equal to another string?

String str = "When is the Spring Festival and autumn? What is the past? The building turned east again last night, and the motherland was overwhelmed by looking back at the moon and tomorrow ";

System. out. println ("length:" + str. length (); // 31

System. out. println ("The fourth character is" + str. charAt (3); // month

System. out. println ("the location of the first comma is" + str. indexOf (','); // 7

System. out. println ("the location of the first comma is" + str. indexOf (","); // 7

System. out. println ("the first past is" + str. indexOf ("past"); // 8

System. out. println ("index of the last month" + str. lastIndexOf ("month"); // 28

System. out. println ("whether it contains the Moon" + str. contains ("the moon"); // true

System. out. println ("whether it starts with spring flowers" + str. startsWith ("spring flowers"); // true

System. out. println ("whether to end with MM" + str. endsWith ("mm"); // true

System. out. println ("is empty" + str. isEmpty (); // false

System. out. println (str. equals ("another string"); // false

String s1 = "abc ";

String s2 = "aBC ";

System. out. println (s1.20.signorecase (s2); // true when comparing signorecase, Case sensitivity is ignored.

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.