First Lecture Overview
1, the string is a special object, once it is initialized, it can not be changed, cannot have subclasses, so the method in string can not be replicated;
2. There are two ways of assigning strings
1) String S1 = "abc"; --there is only one pair of objects in memory.
2) String s2 = new String ("abc");--There are two objects in memory, "ABC" is an object, and new creates an object.
3. Equals method
The Equals method in object is used to compare the addresses of two objects, and the method is overridden in the string class so that it compares the value of a specific object.
Second lecture string Common functions
1. Character of string in memory
2. How to manipulate strings
2.1 Read method
l int Length ()--get string lengths;
L char charAt (int index)--Gets a character from the position according to the position;
l int indexOf (int ch)--Returns the position of the first occurrence of CH in the string;
l int indexOf (int ch,int fromIndex)-Gets the position of the first occurrence of CH in the string, starting at the FromIndex specified position;
l int indexOf (string str)-Returns the position of the first occurrence of STR in the string;
l int indexOf (String str,int FromIndex)-finds the first occurrence of STR in character channeling, starting from the FromIndex specified position;
l int lastIndexOf (int ch)--Returns CH from the last occurrence of the string
2.2 Judging the operation
L Boolean contains (str)--Determines whether the string contains str;
L Boolean isEmpty ();--Determines whether there is content in the string. The principle is to determine whether the string length is 0;
L Boolean startsWith (str)--Determines whether a string begins with Str.
L Boolean endsWith (str)--Determines whether the string ends with Str.
L Boolean equals (str)--Determines whether the contents of the string are the same. The Equals method in the object class.
L Boolean equalsignorecase (str)--Determines whether the content is the same, and ignores the casing.
2.3 strings are converted to other data types
L Convert a string to a string:
Method One: Constructor method
String (char[] ch)
String (char[] Ch,int offset,int count)--Converts a portion of a string array into a string.
Method Two: Static methods
Static String copyvalueof (char[] ch)
Static String copyvalueof (char[], int offset, int count)
L Convert a string into a character array
Char[] ToCharArray ();
L convert byte array to string--constructor method implementation
String (byte[] b);
String (byte[] b,int offset, int count)
L Convert a string into a byte array
Byte[] GetBytes ();
Special case: String and byte data in the conversion process, you can specify the encoding table.
L Convert a basic data type to a string
Static string valueOf (int i)--Converts the int type data to a string
Static string valueOf (double D)--converts the double data to a string
2.4 Replacing a character in a string
String replace (Oldchar, Newchar).
2.5 cutting a string into multiple strings based on a specified character
String[] Split (regex);
2.6 Getting a part of a string
string substring (BEGIN)--gets all characters from the string after the begin.
String substring (begin, end)-Gets the character in the string from begin (contains) to end (not included).
2.7 case conversion, removing whitespace, string natural order comparison
L Uppercase and lowercase conversions
String toUpperCase ()--converts the string to uppercase;
String toLowerCase ()--converts the string to lowercase;
L Remove multiple spaces at both ends of a string
String trim ();
L Comparison of two strings in a natural order
Int compareTo (string);--the string overrides the CompareTo method in the comparable interface.
Practice:
1. Simulate a trim method to remove spaces at both ends of the string
Ideas:
1) Determine if the first position of the string is empty, and if it continues to be judged downward until it is not a space, the space at the end is judged as well.
2) When the beginning and end of the judgment is not a space, the characters between the front and back are removed to achieve the purpose of removing both ends of the string.
The code is implemented as follows:
2. Flip a string. Flips the specified part of a string
3. Gets the number of occurrences of a string in another grandparent string
4. Gets the maximum of the same string in two strings.
Third Speaking StringBuffer
1. Overview
StringBuffer is a container in which we can add, delete, change, and check the data in the container. A string buffer can be used when the data type and number of data are indeterminate, and eventually become a string.
2. Features
1) The length can vary;
2) can directly manipulate multiple data types;
3) will eventually become a string via the ToString method;
3. function
1) Storage
StringBuffer append ()--adds the specified data as a parameter to the end of the existing data
StringBuffer Insert (index, data)-inserts data into the specified position (index);
2) Delete
StringBuffer Delete (int start,int end)-Deletes the data in the buffer, contains start, and does not contain end;
StringBuffer Deletecharat (Index)--Deletes the character at the specified position
3) Get
Char charAt (int index);--Returns the character at the specified position
Int indexOf (String str)--finds the first occurrence of a string str in a container
Int lastIndexOf (String str)--finds the last occurrence of the string str in the container
Int Length ()--Gets the length of the container
String substring (int start, int end)-Gets the data in the buffer from start (contains) to end (not included).
4) Modify
StringBuffer Repace (start,end,string)--replaces the data in the container from start (contained) to end (not included) with a String.
Void setcharat (int index, char ch)--replaces the data at index position in the buffer with Ch.
5) Invert
StringBuffer reverse ();
4, Stringbulder and StringBuffer
Stringbulder is only available after the jdk1.5 version, in order to improve the efficiency of single-threaded operation. The difference between it and StringBuffer is as follows:
1) StringBuffer is thread-synchronous, that is, when multithreading is called, the operation code of the data in the buffer is synchronized, and the Stringbulder is not synchronized.
2) Stringbulder does not need to determine the lock (because of the different steps), improve the operating efficiency, in a single-threaded under the recommended use of Stringbulder, multi-threaded recommended use of StringBuffer.
Fourth Lecture Basic Data type Object wrapper class
1. Overview
Wraps the base data object and its operations.
2, the basic data type packaging class role
The wrapper class appears to compile operations on basic data type data, such as the maximum number of shapes that can be obtained directly from the shaping wrapper class, or the ability to transform other types of data into shaping directly using the methods in the wrapper class. Common basic data Object type wrapper classes correspond to the following:
3. New characteristics of packaging class
1) decimal into other binary
Tobinarystring ()--converts decimal into binary;
Tohexstring ()--converts decimal into 16 binary;
Tooctalstring ()--converts decimal into octal system;
2) other binary into decimal
Integer.parseint (String,radix)-where Radix is the cardinality, if sring is a binary number, radix value is 2, if string is hexadecimal, radix is 16; If string is an octal number, Radix is 8;
3) Automatic box packing/unpacking
Interger x = 4;--This is an auto-boxing process, equivalent to an integer x = new Integer (4);
x = x + 5;--This sentence goes through the process of unpacking and boxing automatically. The x to the right of the equals sign is a reference type data that, when operated, is automatically added to the int type and 5, then boxed, and assigned to the X to the left of the equals sign.
4) The integer object is stored in memory
Integer m = 127;
Integer n = 127;
System.out.println (M = = n);//result is true
Integer a = 128;
Integer B = 128;
System.out.println (A = = B);//result is False
The reason for this difference is that when the value is within the byte range (shaping less than or equal to 127), for the new attribute, if the value already exists, no new space is created, that is, M and N point to the same object.
Summarize
1. String and StringBuffer differences
1) string and StringBuffer have the operation of adding and checking strings;
2) string length and content are immutable, StringBuffer length and content are variable;
3) StringBuffer has reversal function;
2. The main differences between StringBuffer and StringBuilder
1) StringBuffer thread synchronization, StringBuilder thread is not synchronized;
2) StringBuffer efficiency is low, StringBuilder efficiency is high;
3) Multi-threading when using StringBuffer, single-threaded use of StringBuilder;
3. Basic data type Object wrapper class
1) Facilitate the operation of the basic data, if the conversion between the basic data types, such as conversion;
2) Basic data types are also automatic boxing and unpacking functions;
Dark Horse programmer-java Base---String, StringBuffer, StringBuilder, wrapper class