String Class (II)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.