1. Introduction
This blog will introduce the basic knowledge of string classes in Java, some common methods of the main string classes, and so on.
2. Creation of String objects
1. There are two forms, but they are often used in the form of string variable names to operate in development.
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. String;public class Sample13_1 {public static void main (string args[]) {//Use simple syntax to create string object strings S1= "Java Core technology Daquan welcome you!! ";//Use the constructor to create a string object," string S2=new string ("Congratulations on creating a string with two different syntaxes!!) ");//Create an empty string object string s3=" ";//The value of string reference S4 is nullstring s4=null;//print content System.out.println (s1+" # "+s2+" # "+s3+" # "+S4);}} </span>
some important methods of the 2.String class
String as an object's identity, there must be some way to manipulate the string, the common method is summarized as follows.
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. String;public class Sample13_3 {public static void main (string args[]) {string S1 = "JavaSE6.0 core technology Daquan Welcome!! "; String s2 = new String ("Congratulations on creating a string with two different syntaxes!!) "); String s3;//uses the CharAt method to get a character in a string System.out.println ("=====================charat======================="); char C = S1.charat (4); System.out.println ("Starting from 0, the 4th character in string S1 is:" + c);//Use the ToCharArray method to convert a string to a character array System.out.println ("================== ===tochararray================== "); char[] array = S1.tochararray (); System.out.print ("Print the char Array backwards:"); for (int i = array.length-1; I >= 0; i--) System.out.print (Array[i]);//Use concat method to connect Two strings of System.out.println ("\n=====================concat======================="); s3 = S1.concat (S2); SYSTEM.OUT.PRINTLN ("The result of connecting two strings is:" + s3);//Use the Equals method to compare the contents of a two string System.out.println ("=====================equals= ====================== "); s3 =" JavaSE6.0 Core technology Daquan welcome you!! "If (S1.equals (S3)) System.out.println (" Congratulations on your test success, S1 and S3 content is exactly the same!! ");//Use EqualsignorecThe ASE method compares the contents of a two string ("=================equalsignorecase=================") with a case-insensitive System.out.println; s3 = " JAVASE6.0 Core Technology Daquan welcome you!! "; if (S1.equalsignorecase (S3)) System.out.println (" S1 and S3 are the same in case insensitive!! ")///Use the length method to get a string of System.out.println (" ======================length====================== "); int size = S1.length (); System.out.println ("The number of characters in the S1 is:" + size);//Use the Replace method to replace a character in a string System.out.println ("======================replace ===================== "); s3 = S1.replace (' A ', ' X '); SYSTEM.OUT.PRINTLN ("Replace all letters A in S1 with the letter x:" + s3);//Use the substring method to get a substring of the string System.out.println ("====================== substring=================== "); s3 = S1.substring (10); System.out.println ("Starting from 0, the 10th character in the S1 string is:" + s3); s3 = S1.substring (2, 10); System.out.println ("Starting from 0, S1 the 2nd character to the 10th character string is:" + s3);//Use the toLowerCase method to convert the character embox of a string to lowercase System.out.println ("======== ==============tolowercase================= "); s3 = S1.tolowercase (); SYSTEM.OUT.PRINTLN ("Change the word embox in S1 to lowercase:" + s3);//Use the toUpperCase method to convert the character embox of a string to uppercase System.out.println ("======================touppercase================= "); s3 = S1.touppercase (); SYSTEM.OUT.PRINTLN ("Change the word embox in S1 to uppercase:" + s3);//Use the Trim method to remove the invisible character of the string header and tail System.out.println ("====================== trim======================== "); s3 =" Java SE6.0 "; System.out.println ("S3 did not use Trim method before: [" + S3 + "]"); s3 = S3.trim (); System.out.println ("S3 after Trim method: [" + S3 + "]");}} </span>
Memory mechanism for 3.String objects
The string object has the benefit of a "one-time investment, lifetime return", which first analyzes a piece of code
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. String;public class Sample13_4 {public static void main (string args[]) {//Use simple syntax to create a string S1 with s2string S1 = "JavaSE6.0"; String s2 = "JavaSE6.0";//Use the constructor to create a string s3string s3 = new String ("JavaSE6.0");//Use = = to compare strings S1 with s2if (S1 = = s2) {System.out.prin TLN ("string S1 with string s2 passed equals test, result returns true");} else {System.out.println ("string S1 with string S2 does not pass equals test, Result returns false");} Use = = to compare strings S1 with s3if (S1 = = S3) {System.out.println ("string S1 with string S3 Pass equals test, Result returns true");} else {System.out.println (" String S1 with string S3 does not pass equals test, Result returns false "); Use the Equals method to compare strings S1 with S3if (S1.equals (S3)) {System.out.println ("string S1 is the same as string S3 content, the result returns true");} else { System.out.println ("string S1 differs from string s3, Result returns false");}}} </span>
If you allow the above example, there will be unexpected results, the following is a simple analysis of the memory mechanism of the string.
At run time, the JVM divides the memory into two parts, one is a heap, the other is a stack, the object is placed in the heap, and a reference or local variable is placed on the stack. In memory design, Java opens up a small amount of memory in the heap, called a constant pool of strings, to hold a particular string object.
When you create using string s1= "XXXX", you first look at whether the constant pool is already included, and if so, point to the reference directly.
So when you run the above example, you won't be surprised. By this design at the time of the creation of the string has been compared, in the future, only a comparison of two references to the same object can be pointed to. Note that a string object created with new will create a string object directly on the heap without this effect.
3.StringBuffer class
The string class is described above, but there are shortcomings, if the string concatenation of strings, memory consumption will be very large, so in order to compensate for the lack of string, StringBuffer appeared. When stitching strings through StringBuffer, no intermediate objects are produced, so you should use the StringBuffer class when you need to make a large number of connections to the string.
The common methods of the StringBuffer class are as follows
<span style= "font-family:simsun;font-size:18px;" >package com. String;public class Sample13_10 {public static void main (string[] args) {StringBuffer sb1 = new StringBuffer ("Java6.0"); System.out.println ("The contents of the object SB1 before the operation are:" + SB1);//SB1 a series of operations Sb1.append ("Core technology Daquan"). Insert (4, "SE"). Delete (9,.). Reverse (); System.out.println ("The contents of object SB1 after a series of actions are:" + SB1);//Create the same StringBuffer object as SB1 sb2stringbuffer SB2 = new StringBuffer ( Sb1.tostring ());//Use the Equals method of StringBuffer to test System.out.println ("= = = = = = = = = = = = = = = = = = = = = = = = = = = = SB1 if (Sb1.equals (SB2)) {System.out.println ("Object SB1 and SB2 content are the same!!! ");} else {System.out.println ("Object SB1 is not the same as SB2 's content!!! ");} Use the ToString method to test System.out.println with the Equals method ("Use the Equals method after using the ToString method to test the contents of the object sb1 and SB2 = = ="); String S1 = sb1.tostring (); String s2 = sb2.tostring (), if (s1.equals (S2)) {System.out.println ("Object SB1 is the same as SB2 content!! ");} else {System.out.println ("Object SB1 is not the same as SB2 's content!! ");}}} </span>
4.StringBuilder class
The StringBuilder class is similar to the StringBuffer class usage, and can be used to make a large number of string concatenation operations.
The difference between 5.StringBuilder and StringBuffer
1. First, the StringBuilder is more efficient than the StringBuffer class, but the StringBuilder class's string editing methods do not synchronize and cause problems in multi-threading.
2.StringBuffer does not cause problems when multi-threading, because the editing of strings is synchronized
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java reading note Three (string)