Java.lang Package Classes

Java - String valueOf() Method



The Java string valueOf() method returns the string representation of the character subarray 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.

Syntax

public static String valueOf(char[] data, int offset, int count)

Parameters

data specify a character array.
offset specify initial offset of the subarray.
count specify length of the subarray.

Return Value

Returns a String that contains the characters of the specified subarray of the character array.

Exception

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

Example:

In the example below, valueOf() method returns the string representation of the character subarray argument.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    char[] x1 = {'H', 'e', 'l', 'l', 'o'}; 
    System.out.println(String.valueOf(x1, 1, 3));
  }
}

The output of the above code will be:

ell

❮ Java.lang - String