The difference and relation between String and char
char is the basic data type and character type;
definition char You must use single quotes when you type variables'',And can only have one character; char It occupies 2 bytes and adopts 16 bits Unicode Character (i.e UTF16)Code, indicating range: 0~65535;A Chinese occupies 2 bytes, so char One Chinese character can be stored.
As follows:
char c = 'c'; char c = 'in'; // Automatic type promotion, which automatically transforms upward to int type according to ASCII code table char x = 'x'+65415; //The hexadecimal code of x in the ASCII code table is: 120 char d = 'd'+65435; //d in ASCII code, hexadecimal code is 100 char d = 'd'+65436; //Error prompt appears for d Hex= 100, and 100 + 65436 = 65536 exceeds the capacity representation range of char
String is a reference data type and a string type. It is an object decorated with final, so it cannot be inherited. When defining a string type variable, you must use double quotation marks' '.
public String() { this.offset = 0; this.count = 0; this.value = new char[0]; }
String type can be converted to char []. The bottom layer of string is also based on char implementation. It provides a constructor new String(char []) to convert char to string type, and toCharArray() method to convert string to char [].
char[] ch = {'a','b','c'}; String str = new String(ch); System.out.print(ch); // Output: abc char[] tc = str.toCharArray(); for(int i=0;i<tc.length;i++){ System.out.print("-"+tc[i]); } // Output-a-b-c
Difference between new String [] {} and String[] str = {}
String[] policyOrderStatus = new String[]{ "0", "1", "2" };
String[] policyOrderStatus = { "0", "1", "2" };
String[] str = new string [] {} is different from String[] str = {}:
The first is to create a new object with new(), which will be stored in the heap. Each call will create a new object.
The second is to create an object reference variable str of String class in the stack, and then find out whether "0", "1", "2" is stored in the stack,
If not, put "0", "1" and "2" on the stack and make str point to "0", "1" and "2". If there are already "0", "1" and "2", directly make str point to "0", "1" and "2".
Difference between new String(bytes) and byte[].toString()
String byt = "Convert byte Content"; byte[] bytes = byt.getBytes(); System.out.println("bytes: "+bytes); System.out.println("bytes to String: "+bytes.toString()); String s = new String(bytes); System.out.println("bytes to new String: "+s);
The toString() method of array in Java does not return the contents of the array. It actually returns the type of array storage elements and an identification of the location of the array in memory. new String(bytes) returns the contents of the actual array.
Differences between String, StringBuilder and StringBuffer
String is an immutable class. String includes various fields such as length, but the values in these fields cannot be changed.
StringBuilder and StringBuffer are variable. Both classes inherit the AbstractStringBuilder class.
Implementation principle of AbstractStringBuilder: AbstractStringBuilder is used to improve the efficiency of String connection in java. If + is directly used for String connection, the jvm will create multiple String objects, resulting in great performance overhead.
In AbstractStringBuilder, a char array is used to save the string to append. The char array has an initial size. When the length of the append string exceeds the capacity of the current char array, the char array is dynamically expanded, that is, a larger memory space is re applied, and then the current char array is copied to a new location. Due to the tape of re allocating memory and copying The space overhead is relatively large, so every time you re apply for memory space, you apply for more than the current memory space, which is generally twice the size.