Day12
Interview questions
String S1 = "abc";
String s2 = "abc";
System.out.println (S1 = = s2); True compares the address, if the constant pool already has an "ABC" string, then the string address is given directly to S2 instead of the new
System.out.println (s1.equals (S2)); True compares the property values, equal to the
string S1 = new String ("abc"); New 2 objects, one is the "ABC" in the Constant pool, and the other is a string object
*/
/*string S1 = new String ("abc");
String s2 = "abc";
System.out.println (S1 = = s2); False because the new object is in heap memory, not the "ABC" in the constant pool and therefore the address is also different
System.out.println (s1.equals (S2)); True compares the property values, equal to the
*/
/* String S1 = "a" + "B" + "C";
String s2 = "abc";
System.out.println (S1 = = s2); True constant optimization mechanism, S1 compile time to become "ABC" ~
System.out.println (s1.equals (S2)); True
*/
/* String S1 = "AB"; The address of the constant pool
String s2 = "abc"; The address of the constant pool
String s3 = s1 + "C"; The StringBuffer object is created first, and the address of the ToString object is assigned to the address in the heap where s3,s3 is stored.
System.out.println (s3 = = s2); False
System.out.println (s3.equals (S2)); True
*/
String constructor:
* Public String (): Empty construct
* public string (byte[] bytes): Convert byte array to string
* public string (byte[] bytes,int index,int length): Converts a portion of a byte array (length as long as the index starts) to a string
* public string (char[] value): Convert character array to string
* public string (char[] Value,int index,int count): Turns a part of a character array into a string
* public string (string original): Convert string constant value to string
The difference between empty strings and null: An empty string ("") is a string constant that is also an object of the string class, since the object can of course call a method in the String class
Null is an empty constant, you cannot call any method, otherwise a null pointer exception appears, and a null constant can assign a value to any reference data type
String Common methods:
1. Judgment:
* Boolean equals (Object obj): Compares the contents of strings for the same, case-sensitive
* Boolean equalsignorecase (String str): Compares the contents of a string for the same, ignoring case
* Boolean contains (String str): Determines whether a large string contains a small string
* Boolean StartsWith (String str): Determines whether a string begins with a specified string
* Boolean EndsWith (String str): Determines whether the string ends with a specified string
* Boolean isEmpty (): Determines whether the string is empty.
2. Get
* int Length (): Gets the length of the string.
* Char charAt (int index): Gets the character at the specified index position
* int indexOf (int ch): Returns the index of the first occurrence of the specified character in this string.
* int indexOf (string str): Returns the index of the first occurrence of the specified string in this string.
* int indexOf (int ch,int fromIndex): Returns the index at the first occurrence of the specified character from the specified position in this string.
* int indexOf (string str,int fromIndex): Returns the index at the first occurrence of the specified string from the specified position in this string.
* LASTINDEXOF (int ch,int fromIndex)
* LASTINDEXOF (String str,int fromIndex)
* String substring (int start): Intercepts the string starting at the specified position, from the default to the end.
* String substring (int start,int end): Intercepts the string from the specified position to the specified position.
3. Conversion:
* byte[] GetBytes (): Converts the string to a byte array.
* char[] ToCharArray (): Converts a string to a character array.
* Static string ValueOf (char[] CHS): Turns the character array into a string.
* Static string valueOf (int i): turns data of type int into a string.
* Note: The valueof method of the string class can turn any type of data into a string.
* String toLowerCase (): Turns the string into lowercase. Know
* String toUpperCase (): Turns the string into uppercase.
* String concat (String str): Concatenation of strings.
Java12. API