String ah String

xiaoxiao2021-03-05  22

first part

JAVA has time, it seems that it seems to start into the door, with some feelings but find a lot of confusion and doubts and all from the most basic knowledge, I have checked a check, finally the special object of String this special object I have a little feeling that everyone will take a look at a strange program: public class teststring {public static void main (string [] args) {string s1 = "monday"; string s2 = "monday";}} This program is really simple. ! But what is the problem? 1. What are the objects from String's worries above this program? Maybe many people out: two, s1 and s2? String is a Final class, which is not variable.

It seems that it seems to be very reasonable, then check it out, change the program slightly to see the result: public class teststring {public static void main (string [] args) {string s1 = "monday"; string s2 = " "; If (S1 == S2) System.out.Println (" S1 == S2 "); Else System.out.println (" S1! = S2 ");}} Oh, many people will say more than two An object compiled and runs the program, output: S1 == S2! Why S1 == S2? == Dictionary is said: S1 and S2 reference the same String object - "Monday"! 2. Thousands of string String a little changed, there will be more strange discovery: public class teststring {public Static void main (string [] args) {string s1 = "monday"; string s2 = new string ("monday"); if (S1 == S2) System.out.println ("S1 == S2"); ELSE System.out.println ("S1! = S2"); if (S1.Equals (S2)) System.out.Println ("S1 Equals S2"); Else System.Out.println ("S1 Not Equals S2") We will use the new operator to create a program output: S1! = S2S1 Equals S2 Well, it is clear that S1 S2 references two "Monday" String objects, but why is the two programs different? 3. Swim in the String's swimming pool, turned over the book, finally found the answer: Originally, the program creates a string buffer pool when using S2 = "monday" when creating a string, The program first looks for the object of the same value in this String buffer pool. In the first program, S1 is first placed in the pool, so when S2 is created, the program finds S1 with the same value to reference S1 In the second section of the object "Monday", the new operator is used, and he understands the procedure: "I want a new! Don't be old!" And a new "Monday" Sting object was created. Memory. Their value is the same, but the location is different, and one is swimming in the pool on the shore.

Oops, it's really a waste of resources. What is the same as the same thing? 4. Continue diving to change the program: public class teststring {public static void main (string [] args) {string S1 = "monday"; string s2 = new string ("monday"); s2 = s2.intern (); if (S1 == S2) System.out.Println ("S1 == S2"); Else System.Out.println ("S1! = S2"); if (S1.Equals (S2)) System.out.Println "S1 Equals S2"); Else System.Out.println ("S1 Not Equals S2");}} This time you join: S2 = s2.intern (); wow! Program output: S1 == S2S1 Equals S2 It turned out that after the program has built S2, it is used in INTERN () to overtight him in the pool. This time S2 and S1 have a reference to the same object. We success reduced memory. Occupy 5. == String string with equals () is an object, to compare whether the value of two different String objects is identical to the equals () method, but if there is so many String objects in the program, there is So many times to use Equals, oh, God, really slow, better ways: put all String all in () to the buffer pool, it is best to do this when using New S2 = New String ("Monday"). Intern (); um, are you soaked in the pool? Haha now I can use == to compare the value of the String object is really cool, and it is quick and convenient! the second part

String ah String, let me say what are you? Do you have any trouble for our Java programmers? Look at the String this time, how do you make things? Review the bad temper String Bacad routine 1: Class str {public static void main (String [] args) {string s = "hi!"; String t = "hi ! "; If (s == t) System.out.Println (" equals "); else system.out.println (" not equals ");}} What is the output? If the visitor has seen one of my "confusion from String", I believe you will make the right judgment: program output: equals2. Oh, God, it is stirring with routine 2: Class str {public Static void main (string [] args) {string s = "hello"; string t = s.touppercase (); if (s == t) System.out.Println ("equals"); else system.out.println ("not equals");}} What is the output of this program? careful! Reproduce! Don't be confused by String this fascinating guy! It outputs: Equalswhy !!! Simple program simply change: Class str2 {public static void main (string [] args) {string s = "hello"; string t = S.touppercase (); if (s == T ) System.out.println ("equals"); Else System.Out.println ("not equals");}} You may say: Isn't it the same? Do not! Thousands of true, not the same! This time the output: NOT Equalsoh mygod !!! Who will teach this string! 3. Do you know your horse? "To tame the wild horses, you must understand its bouting" "denim said. Do you know String? Interpret the String API, you can see: TouPperCase () and TOLOWERCASE () method Returns a new String object, which indicates the original string to the uppercase or lowercase situation of the string; however, pay attention: If the original string itself is uppercase Or underground form, then return to the original object.

That's why S and T is entangled in the second program, I have to treat this naughty, and I don't change the String. I don't seem to have a better way to let us dissect it, see what structure it is: (1) Charat (int N) Returns the characters of the N position in the string, the first character position is 0, the last character position is length () - 1, the location of the access error will thrown out a big brick: StringIndexoutofboundsexception is really big enough ( 2) Concat (String STR) Connect a STR after the original object, but returns a new String object (3) EqualsignoreCase (String Str) ignores the substantor of the pre-write Equals method This method is first calling static character methods TOUPERCASE () or TOLOWERCASE () Two character conversion of the contrast, then perform == operation (4) Trim () returns a new object, which cuts off the start of the original object and the blank character of the end of the end, if the result is not different from the original object Return to the original object (5) TOSTRING () String class also has a toString () method? It's a fun problem, but if you don't have it, your String object can not be used in system.out.println (), be careful, it returns to the object's own String class There are many other ways, master them will bring Many convenient things will also have a lot of confusion, so stick to principles, is the most critical 4. I want to buy a better Malay to buy more tamed genius StringBuffer, there will be someone in this time, it is very easy to use, it How can it be the younger brother? Very simple, its interaction function is less than String, if you want to edit the string it is not convenient, you will be disappointed with it but this doesn't mean it is not strong public final class string IMPLEments Serializable, Compailable, CharsequencePublic Final Class StringBuffer IMPLEMENTS SERIALIZABLE, CHARSEQUENCE is obvious, the younger brother has some stuff, but this will not interfere with its future StringBuffer is not inherited by String but also pay attention to the brothers. It is also final, this is the same root to see his method, so More and more stable and reliable methods, use more than naughty String to have efficient multi? Br /> Java provides independent StringBuffer classes for a string object that needs to be changed, and the reason why they want to separate them. Because, the changes in string require the system's opening, and more space is more complicated. I believe that there are more than 10,000 people swim in a narrow pool and wait 10,000 people waiting to enter the swimming pool. When the fire is lively, you will watch the traffic, your String pool administrator will also focus on the situation you don't need to change the string, the simple String class is enough to make you, and when you want to change characters When the content of the string, it is necessary to use the StringBuffer that can support the boat in the stomach. 5. Prime Minute belly can support the boat (1) Length () and Capacity () String Length () returns the length of the brothers StringBuffer This is also true, what do they determine if Capacity () is included by the characters contained in the object? Public class testcapacity {public static void main (String [] args) {

转载请注明原文地址:https://www.9cbs.com/read-36469.html

New Post(0)