Object oriented programming experiment -- string

Posted by pugs1501 on Mon, 31 Jan 2022 14:10:41 +0100

Object oriented programming experiment -- string

Experimental content

1. Write the program to complete the following functions: output the length of the string "www.google.com", calculate and display the number of 'o' and 'g' respectively, and intercept "Google" for output and display.
2. Write a program and try to compare whether "Hello java" and "Hello java" are equal with "= =" and equals() methods.
3. Use String and StringBuffer classes to link strings, analyze and compare their characteristics.

Problem description

1. Write the program to complete the following functions: output the length of the string "www.google.com", calculate and display the number of 'o' and 'g' respectively, and intercept "Google" for output and display.

package day02;
public class google{

    String str="www.google.com"; 
    int oCount,gCount;
    public void method(){ 
        for(int i=0;i<str.length();i++)
        { 
        if(String.valueOf(str.charAt(i)).equals("o"))
        {
             oCount++;
        }
        if(String.valueOf(str.charAt(i)).equals("g"))
        {
             gCount++;
        }
      } 
     System.out.println("www.google.com The length of the is:"+str.length());
     System.out.println("o The number of is:"+oCount);
     System.out.println("g The number of is:"+gCount);
     System.out.println(str.substring(4,10));
     System.out.println("\n");
}
     public static void main(String args[]) 
     {
         new google().method();
         @SuppressWarnings("unused")
		 String str1 ="Hello java";
        String str2 ="Hello java";
        System.out.println(str1.equals(str2));
        System.out.println(str1==str2);
     }
}

2,3,

package day02;
public class StrCopy {
	private String s1 = "hello ",
			       s2 = "word ",
			       s3 = "hello ",
			       s4 = "java";  //Define string
	private String str;
	private StringBuffer strBuffer = new StringBuffer();  //Create a StringBuffer buffer object
	public static void main(String[] args) {
		new StrCopy().demo();
	}
	
	public void demo() {
	
    	long m1 = System.currentTimeMillis();  //Record the time before the String link
   		str=s1+s2+s3+s4;//Character splicing
     	
   		long m2 = System.currentTimeMillis();  //Record the time after String link
   		
   		long m3=m2-m1;  //Calculate link time
   		System.out.println(str);
   		System.out.println("string Time to splice strings:"+m3);
   		System.out.println("*******************");
   		
   		long m4 = System.currentTimeMillis();  //Record the time before StringBUffer splicing	
   		strBuffer.append(s1).append(s2).append(s3).append(s4);  //Concatenate strings into buffers
   		long m5 = System.currentTimeMillis();  //Record the time after StringBUffer splicing
   		long m6=m5-m4;
   		
   		System.out.println(strBuffer);
   		System.out.println("stringBuffer Time to splice strings:"+m6);
       }
}

Experimental summary

1. Differences and relationships among String, StringBuffer and StringBuilder:
(1) The same thing: they can all be used to store strings;
(2) Differences: String is immutable; StringBuffer and StringBuilder are variable, but StringBuffer is thread safe and StringBuilder is unsafe;
(3) String splicing is actually to recreate a new object, and then let the reference re quality this new object. Each time the string type is changed, it is actually equivalent to generating a new string object, and then pointing the pointer to the new string object. Therefore, it is better not to use string for strings that often change content. StringBuffer and StringBuilder reinsert characters based on the current string without relocating. Each result will operate on the StringBuffer object itself instead of generating a new object and changing the object reference. Therefore, it is recommended to use StringBuffer when string objects often change. Therefore, strCpy2 implemented with StringBuffer is more efficient than strCpy1 implemented with string, and string splicing with StringBuffer and StringBuilder is faster than string;
(4) StringBuffer and StringBuilder share the same API, but the former is more thread safe than the latter, but the string splicing speed of the latter is higher than that of the former.

Topics: Java