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.



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.


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:

Snowflake Cloud Data Warehouse

What is Snowflake, and what can you do with Snowflake? In video article.

Learning Journal

Array Concatenation in Java

Learn how to join two or more arrays together in Java by various methods.

Learning Journal

What is Programming and Programming Language

"PROGRAMMING" is the word which we all come across quite often. Let us see what that means.

Learning Journal

Generate Random Number in Java

Look at the different methods to generate random numbers in Java.

Learning Journal

Dawn of Bigdata

Let us go through the birth of Bigdata.

Learning Journal