constructor function
- Public String (Char value[])
Source
Public String (char value[]) {int size = Value.length;this.offset = 0;this.count = Size;this.value = arrays.copyof (value, s ize); }
This is the use of arrays class in the Copyof method, continue to look at the source of this method
public static char[] CopyOf (char[] original, int newlength) { char[] copy = new Char[newlength]; System.arraycopy (original, 0, copy, 0, math.min (Original.length, newlength)); return copy; }
Math's Min method source code
public static int min (int a, int b) {return (a <= b)? A:b; }
Now look from the bottom up, system.arraycopy (original, 0, copy, 0, Math.min (original.length, newlength)); This code is a copy array, Assigns the original array to the copy array. A test should be done on this method:
public static void Main (string[] args) {char[] oraginal ={' B ', ' C ', ' d ', ' E '};char[] copy = new char[4]; System.arraycopy (oraginal, 1, copy, 0, 3); for (int i=0;i<copy.length;i++) {System.out.print (copy[i]+ "");}}
Above is the test public static native void Arraycopy (object src, int srcpos, object dest, int destpos,int length); Method code
This method is the native method and we do not see the Origin code. This
SRC is the source array, Srcpos source array starting position, dest destination array, destpos destination array starting position, length needs to be copied.
Continue to see the constructor of this introduction, that is, when the constructor is called, the incoming char[] is assigned to the Value property of the string object, and the Offest and count of the string property are changed
String Class (II)