And then the front object-oriented ~ today is about the string class: In fact, the string class also contains a lot of object-oriented knowledge ~
First of all, ask a question: we in the development process, if you want to use a class, it is necessary to create objects, this sentence is no problem ~ ~ in the actual development of the time is really this, only to create an object to really use a common class, we generally create objects, Almost all class creation objects are created with the New keyword ~
Here's the problem. Why should our string be written directly as String str = "ABC";
Of course, the string class can also create objects by using new ...
In fact, it is not difficult, we see its source code description will know:
* Strings is constant; Their values cannot is changed after they * is created. String buffers support mutable strings. * Because String objects is immutable they can be shared. For example: * <p><blockquote><pre> * String str = "abc"; * </pre></blockquote><p& Gt * is equivalent to: * <p><blockquote><pre> * char data[] = {' A ', ' B ', ' C '}; * String str = new S Tring (data); * </pre></blockquote><p>
Enter the source string, the first can see the above text: the approximate meaning is that the object value of string is actually stored through the char[] array, string str = "abc"; equals char data[] = {' A ', ' B ', ' C '}; String str = new string (data); clear.
1, the definition of the string class:
Public final class String
Question: Can we inherit the string class during the development process?
Answer: No, we look at the definition, we know that the definition ofstring uses the final modifier, which is defined in order to terminate the class, so string cannot be extended.
Implements: This keyword is used to indicate the interface function, meaning that serializable, comparable, and charsequence are all interfaces.
View Interface Source code:
Comparable<t>: Represents the Comparator function,<T> takes the form of Java generics
Serializable:
Public interface Serializable {}
We can see what's inside the serializable interface, so why do we have to implement this interface? What is the function of this interface?
Role: Serialization of Java classes, as described in more detail later. In fact, serializable is a markup interface in Java, What is called the tag interface? Serializable just as a token of the role of its specific function and function to the JVM to implement the implementation of this interface, just notify jvm,string This class can be serialized.
Charsequence: denotes the character sequence bar ~ This is nothing to say, the way the string is stored depends on the character sequence's ~
2. Properties
Private final char value[];//string value is the private int hash stored on value[] ;//default to 0 means hash value, private static Final long Serialversionuid = -6849794470754667710l;//serialization function private static final objectstreamfield[] Serialpersistentfields = New objectstreamfield[0];//serialization action
As long as we know the properties of the string class, it's almost useless, because it's all defined as private and privatized.
3. Construction method
Here is also only a few special, generally in the development process will not be used in the construction method, because very little use of new to create a string object, are directly using string str = "ABC", such a form of the child:
In the course of the interview, many interviewers will ask the string str = new String ("abc"); How many objects does this create? As for the answer and explanation, this side does not introduce, want to know to go online to find out ~ This involves the underlying memory problem, the description of the length will be very long ~
public string (string original) { this.value = Original.value; This.hash = Original.hash; }
As can be seen from the above, the string two core is value and hash, these two values basically determines the string object.
Public String (char value[]) { this.value = arrays.copyof (value, value.length); }
This is created in the form of a copy of the underlying array, in fact
The underlying use of the System.arraycopy () method, this method uses the native modified, the bottom layer is implemented by C + +, in fact, the role of a copy。
Public String (byte bytes[], int offset, int length) { checkbounds (bytes, offset, length); this.value = Stringcoding.decode (bytes, offset, length); }
This means that the string is constructed in bytes (byte), but the underlying implementation is also through the System.arraycopy () method.
The rest of the basic is nothing to say, to know about it. We can see that some constructs use @deprecated annotations, which are deprecated, and are not recommended for use in this way .
4. Common methods
public int Length () {//returns string length return value.length; } public Boolean isEmpty () {//judgment string is not empty return value.length = = 0; } public char charAt (int index) {//a character in the string, index denotes the position of the character if (Index < 0) | | (index >= value.length)) { throw new stringindexoutofboundsexception (index); } return value[index]; } Public byte[] GetBytes () {//Get byte array return Stringcoding.encode (value, 0, value.length); } public boolean equals (Object anobject) {///string comparison, we know that the comparison of basic types is done by = =, but the string needs to be compared using this method
we are mainly to verify the = = and Equals method
Package Me.javen.oop;public class Stringdemo {public static void main (string[] args) {String str1 = "Hello";//Direct Assignment String str2 = new String ("Hello"),//value string STR3 = str2 via new,//Pass reference System.out.println ("str1 = str2--" + (STR1==STR2) );//FalseSystem.out.println ("str1 = = STR3--" + (STR1==STR3));//FalseSystem.out.println ("str2 = = STR3" + (STR2==STR3)) ;//true//The following by the Equals method System.out.println ("str1 equals str2-to" + (Str1.equals (str2)));//TrueSystem.out.println (" STR1 equals STR3--"+ (Str1.equals (STR3)));//TrueSystem.out.println (" str2 equals STR3--"+ (Str2.equals (STR3 )));//True}}
There is also a comparison method that is used more than one:
public boolean equalsignorecase (String anotherstring) {//case-insensitive comparison
public boolean startsWith (string prefix) {//Determines whether the string starts with prefix public boolean endsWith (string suffix) {// Determines if a string is not ending with suffix public int hashcode () {//Gets the hash value of the string public int indexOf (string str) {//Gets the starting position of the character str,-1 means no this character return indexOf (str, 0); } public string substring (int beginindex, int endIndex) {//Intercept string Beginindex represents the starting position, EndIndex represents the end position public string ReplaceAll ( String regex, String replacement) {//String substitution replacement replace Regexpublic string[] Split (string regex) {//delimited string, Regex represents the delimiter public string toLowerCase () {//Converts the string all to lowercase public string touppercase () {//Converts the string all to uppercase public string trim () {// Remove the leading and trailing spaces, the space in the middle of the character cannot be removed public static string valueOf (Object obj) {///obj is converted to a string, this can be passed any type, such as number, character, floating point, etc.
In addition, there are many other methods, this is not listed here, learners can use the code of the way to using these methods, listed in the development of the commonly used methods ~