Java Self-learning - Numbers and String Strings

String in Java

Example 1: Creating strings

Strings are combinations of characters. In Java, strings are a class, so all strings we see are objects.
Common string creation methods:

  1. Whenever a literal value appears, the virtual machine creates a string
  2. Call String's constructor to create a string object
  3. String splicing with + plus sign also creates new string objects

     public class TestString {
    
         public static void main(String[] args) {
             String garen ="Galen"; //Literal value. When the virtual machine encounters literal value, it creates a string object.
    
             String teemo = new String("Ti Mo"); //Two string objects are created
    
             char[] cs = new char[]{'Cui','Si','special'};
    
             String hero = new String(cs);//  Create a string object from an array of characters
    
             String hero3 = garen + teemo;//  String splicing by + plus sign
         }
    }

Example 2: final

String is modified as final, so it cannot be inherited

package character;
 
public class TestString {
 
    public static void main(String[] args) {
        MyString str = new MyString();
         
    }
     
        /*Errors will be reported here because String cannot be inherited*/
    static class MyString extends String{
         
    }
     
}

Example 3: immutable

Immutable means immutable
For example, create a string object
String garen = Galen;
The specific meaning of immutability refers to:
Can't increase length
Can't Reduce Length
Cannot insert characters
Cannot delete characters
Characters cannot be modified
Once the string is created, the contents will never change.

String behaves like a constant

package character;
  
public  class TestString {
  
    public static void main(String[] args) {
        String garen ="Galen";
         
    }
}

Example 4: String formatting

If you don't use string formatting, you need to connect strings. If you have more variables, splicing will become cumbersome.
Using string formatting, you can be concise and clear

package character;
   
public class TestString {
   
    public static void main(String[] args) {
      
        String name ="Galen";
        int kill = 8;
        String title="Holy shit(dota), legendary(lol)";
          
        //Direct use + string connection, coding will feel cumbersome, and poor maintenance, poor readability.
        String sentence = name+ " Continuous " + kill + " After the second kill, he got it. " + title +" Designation";
          
        System.out.println(sentence);
         
        //format string
        //% s denotes a string,% d denotes a number, and% n denotes a newline
        String sentenceFormat ="%s Continuous %d After the second kill, he got it. %s Designation%n";
         
        String sentence2 = String.format(sentenceFormat, name,kill,title);
         
        System.out.println(sentence2);
         
    }
}

Example 5: String length

The length method returns the length of the current string
You can have a zero-length string, an empty string

package character;
   
public class TestString {
   
    public static void main(String[] args) {
  
        String name ="Galen";
         
        System.out.println(name.length());
         
        String unknowHero = "";
         
        //You can have a zero-length string, an empty string
        System.out.println(unknowHero.length());
          
    }
}

Practice: String Array Sorting

Create an array of strings with length 8
Initialize this array with eight random strings of length 5
Sort the array by the first letter of each string (regardless of case)

Note 1: You can't use Arrays.sort() to write by yourself
Note 2: Ignore case, that is, Axxxx x and axxxxx have no order

Answer:

package character;
 
import java.util.Arrays;
 
public class TestString {
 
    public static void main(String[] args) {
 
        String[] ss = new String[8];
        for (int i = 0; i < ss.length; i++) {
            String randomString = randomString(5);
            ss[i] = randomString;
        }
        System.out.println("Unordered array of strings:");
        System.out.println(Arrays.toString(ss));
 
        for (int j = 0; j < ss.length; j++) {
            for (int i = 0; i < ss.length - j - 1; i++) {
                char firstChar1 = ss[i].charAt(0);
                char firstChar2 = ss[i + 1].charAt(0);
                firstChar1 = Character.toLowerCase(firstChar1);
                firstChar2 = Character.toLowerCase(firstChar2);
 
                if (firstChar1 > firstChar2) {
                    String temp = ss[i];
                    ss[i] = ss[i + 1];
                    ss[i + 1] = temp;
                }
            }
        }
 
        System.out.println("Sorted string array:");
        System.out.println(Arrays.toString(ss));
 
    }
 
    private static String randomString(int length) {
        String pool = "";
        for (short i = '0'; i <= '9'; i++) {
            pool += (char) i;
        }
        for (short i = 'a'; i <= 'z'; i++) {
            pool += (char) i;
        }
        for (short i = 'A'; i <= 'Z'; i++) {
            pool += (char) i;
        }
        char cs[] = new char[length];
        for (int i = 0; i < cs.length; i++) {
            int index = (int) (Math.random() * pool.length());
            cs[i] = pool.charAt(index);
        }
        String result = new String(cs);
        return result;
    }
}

Keywords: Java

Added by gofeddy on Thu, 03 Oct 2019 18:20:42 +0300