Java String copyValueOf​ method example - Internal Implementation

Java String copyValueOf​ method:

copyValueOf​ method is present in the String class which is in package java.lang.String. copyValueOf​ is used to convert character array into String that means it creates a new String with contents of array elements. In this tutorial, We will learn String copyValueOf​ method syntax, String copyValueOf​ method example and String copyValueOf​ method internal implementation.

String has overloaded copyValueOf​ methods and both are static methods which can be accessed by class name instead of creating object for String class.



copyValueOf​ Syntax 1: 

public static String copyValueOf​(char[] data);


Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the returned string.


String copyValueOf​ method


Parameters:

data - the character array.

Returns:

A String that contains the characters of the character array. 

String copyValueOf​ method example 1:


package examples.java.w3schools.string;

public class StringCopyValueOfExample {
public static void main(String[] args) {

char[] c = { 'j', 'a', 'v', 'a' };

String str = String.copyValueOf(c);

System.out.println("Copy of array as String : " + str);
}
}


String copyValueOf​ method example 1 Output:

The above program produces the following output.


Copy of array as String : java


Example: What does happen if char array is null?


String str = String.copyValueOf(null);
If char array is null then internally it tried to get the length of the char array length. But in this case, it is null. Hence, will through NullPointerException.


Null case Output:


Exception in thread "main" java.lang.NullPointerException

at java.base/java.lang.String.(String.java:251)
at java.base/java.lang.String.copyValueOf(String.java:3017)
at w3schools/examples.java.w3schools.string.StringCopyValueOfExample.main(StringCopyValueOfExample.java:7)


copyValueOf​ Syntax 2 with offset: 


public static String copyValueOf​(char[] data, int offset, int count);

Returns the string representation of a specific subarray of the char array argument.

The offset argument is the index of the first character of the subarray. The count argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the returned string.



Parameters:

    data - the character array.
    offset - initial offset of the subarray.
    count - length of the subarray.


Returns:

A String that contains the characters of the specified subarray of the character array.


Throws:

IndexOutOfBoundsException - if offset is negative, or count is negative, or offset+count is larger than data.length. 


copyValueOf​ Syntax 2 with offset Example:

Now, consider the following is the char array.


char[] c = { 'j', 'a', 'v', 'a', '-', 'w', '3', 's', 'c', 'h', 'o', 'o', 'l', 's'};

We want to create the new string for "w3schools" which starts index at 5, need to get 9 characters from 5th position. So, here offset is 5 and count is 9. Refer the below code for offset example.



public class StringCopyValueOfOffsetExample {
public static void main(String[] args) {

char[] c = { 'j', 'a', 'v', 'a', '-', 'w', '3', 's', 'c', 'h', 'o', 'o', 'l', 's' };
String strOffset = String.copyValueOf(c, 5, 9);

System.out.println("Copy of array as String using offset and count arguments: " + strOffset);
}
}


copyValueOf​ Syntax 2 with offset Example Output:

The above program produces the following output.

Copy of array as String using offset and count arguments: w3schools

String copyValueOf​ IndexOutOfBoundsException Example:


There are some scenarios it can trigger IndexOutOfBoundsException in the following cases.



1) If offset is negative
2) If count is negative
3) If offset+count is larger than char array length.


Case 1 If offset is negative:


Here, offset value is -2 which is negative that would through IndexOutOfBoundsException.

String strOffset = String.copyValueOf(c, -2, 9);

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: offset -2, count 9, length 14
at java.base/java.lang.String.checkBoundsOffCount(String.java:3304)
at java.base/java.lang.String.rangeCheck(String.java:280)
at java.base/java.lang.String.(String.java:276)
at java.base/java.lang.String.copyValueOf(String.java:3006)
at w3schools/examples.java.w3schools.string.StringCopyValueOfOffsetExample.main(StringCopyValueOfOffsetExample.java:7)


Case 2 If count is negative:

String strOffset = String.copyValueOf(c, 2, -9);

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: offset 2, count -9, length 14
at java.base/java.lang.String.checkBoundsOffCount(String.java:3304)
at java.base/java.lang.String.rangeCheck(String.java:280)
at java.base/java.lang.String.(String.java:276)
at java.base/java.lang.String.copyValueOf(String.java:3006)
at w3schools/examples.java.w3schools.string.StringCopyValueOfOffsetExample.main(StringCopyValueOfOffsetExample.java:7)

Case 3 If offset+count is larger than char array length:

String strOffset = String.copyValueOf(c, 5, 90);


In this case, char array length is 14 but offset+count = 95 which is more than array length.

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: offset 5, count 90, length 14
at java.base/java.lang.String.checkBoundsOffCount(String.java:3304)
at java.base/java.lang.String.rangeCheck(String.java:280)
at java.base/java.lang.String.(String.java:276)
at java.base/java.lang.String.copyValueOf(String.java:3006)
at w3schools/examples.java.w3schools.string.StringCopyValueOfOffsetExample.main(StringCopyValueOfOffsetExample.java:7)


String copyValueOf​ method internal implementation or How String copyValueOf method works inernally:


First, We will see the internal code for these two methods.


1) copyValueOf(char data[]):



    public static String copyValueOf(char data[]) {
return new String(data);
}


2) copyValueOf(char data[], int offset, int count):


    public static String copyValueOf(char data[], int offset, int count) {
return new String(data, offset, count);
}


These two methods have internally creating string object directly calling constructor. This is very straight forward to understand about internal implementation and how String copyValueOf method works internally. Also, same implementation in java 9 and java 11 versions.


Please leave your questions in comments section. We will reply to your queries.

0 Comments