Overview of String: in java, strings in double quotation marks belong to String class objects.
Characteristics of string: once created, it cannot be changed, but can be referenced. String effect is equivalent to character array (char []), but the underlying principle is byte array (byte []).
For example: public class StringDemo01{
public static void main(String[] args) {
//public String(): create a blank string object without any content
String s1 = new String();
System.out.println("s1:" + s1);
//public String(char[] chs): creates a string object according to the contents of the character array char[] chs = {'a', 'b', 'c'}; String s2 = new String(chs); System.out.println("s2:" + s2); //Public string (byte [] bytes): creates a string object according to the contents of the byte array byte[] bys = {97, 98, 99}; String s3 = new String(bys); System.out.println("s3:" + s3); //String s = “abc”; Create a string object by direct assignment. The content is ABC String s4 = "abc"; System.out.println("s4:" + s4); }
}
Differences in string creation:
There are two ways to create a string: (1) the first: for example, String s ={"abc}, which is direct assignment. What are the advantages of this! 1: it does not create a library memory in the heap memory. If there are the same characters, they can share a library memory.
(2) Second: create a new object:
char[]chs={'a,'b,c'}
String s=new String (chs);
This new creates a new object in heap memory.
==Function of No
-
Compare basic data types: specific values are compared
-
Compare reference data types: object address values are compared.
-For example
public class StringDemo02 {
public static void main(String[] args) {
//Get the object by constructing the method
char[] chs = {'a', 'b', 'c'};
String s1 = new String(chs);
String s2 = new String(chs);//Get the object by direct assignment String s3 = "abc"; String s4 = "abc"; //Compare whether the address of the string object is the same System.out.println(s1 == s2); System.out.println(s1 == s3); System.out.println(s3 == s4); System.out.println("--------"); //Compare whether the string contents are the same System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println(s3.equals(s4));
}
}
What are the methods of the String class**
These are the most commonly used methods. If you want to know more, you can query them through the API.
StringBuilder:
Overview: StringBuilder is a variable string class. We can regard it as a container. The variable here means that the content in the StringBuilder object is variable
The difference between and string: first, the content of the former is variable, but string is immutable.
StringBulider: his two common methods,
append: indicates to add data and return the object itself,
reserve: returns the reverse character order
Conversion between and String: public class stringbuilderdemo02{
public static void main(String[] args) { /* //StringBuilder Convert to String StringBuilder sb = new StringBuilder(); sb.append("hello"); //String s = sb; //This is wrong //public String toString(): You can convert StringBuilder to String by toString() String s = sb.toString(); System.out.println(s); */ //Convert String to StringBuilder String s = "hello"; //StringBuilder sb = s; / / this is wrong //public StringBuilder(String s): you can convert a String into a StringBuilder by constructing a method StringBuilder sb = new StringBuilder(s); System.out.println(sb);
Student management system
In addition to defining attributes, we should also list its option bar: there is exit in the option bar, so this is a circular system, so it is like this.
It shows that
Here is a String type, so any number of output will not be affected; therefore, such a toolkit is used in the customer management system.
Able to control input irregularities.
Five options are connected with five methods;
First add student
Here, it is applied to the add function of the set, which takes the input once as an element in the set.
The second delete student
First, I want to find the student's student number, and then look for the same in the traversal set. If it is the same for so long, find his student number and carry out arry.get(). Find it and delete it
3 modify students
To modify, first find the position and then enter the replacement manually. The set method is used
4 view students
Viewing is traversing the collection,
5 end
This is the simplest
Of course, you can also choose one more layer.
The fifth stage is the general understanding of String and collection