copyValueOf() - String Methods in Java


This article is a part of String Methods in Java.
The copyValueOf() method returns a string that represents the characters defined in a character array. There are three parameters in this method, the first one specifies the character array, the second one specifies the starting index of the character array, and finally the third one indicates the ending index of the character array.


Return type

The return type for copyValueOf() method is String. Let us look at an example for this method.

Example


Output

Let us look at the output of the above example code.

String Methods copyValueOf()
Fig.1- String Method - copyValueOf()

Here in the first line the copyValueOf() method works on the character array cs, from the index 0 to 5. Whereas, in the second line only the character array is specified, so the method takes the starting index as 0 and the ending index as length() - 1 by default.

StringOutOfBound exception in copyValueOf()

When the starting or the ending index is a negative number or a number outside the range of the string, copyValueOf() method throws an exception. Let us look at the example code to understand it better.


Output

Let us look at the output of the above example code.

String Methods copyValueOf() StringOutOfBound
Fig.2- String Method - copyValueOf() StringOutOfBound

Here the ending index 10 is way beyond the range of the character array cs.

Read More

Author : Satyam Kumar -



You will also like: