Facebook Page Twitter Page LinkedIn Page
× Java Tutorial Java Advanced Java Resources


The Java string valueOf() method returns the string representation of the boolean, char, character array, character subarray, double, float, int, long or object argument.

Syntax

public static String valueOf(boolean arg)
public static String valueOf(char arg)
public static String valueOf(char[] arg)
public static String valueOf(char[] arg, int offset, int count)
public static String valueOf(double arg)
public static String valueOf(float arg)
public static String valueOf(int arg)
public static String valueOf(long arg)
public static String valueOf(Object arg)

Parameters

arg specify a boolean, char, character array, double, float, int, long or object.
offset specify initial offset of the subarray.
count specify length of the subarray.

Return Value

Returns the string representation of the boolean, char, character array, character subarray, double, float, int, long or object argument.

Exception

throws IndexOutOfBoundsException, if offset is negative, or count is negative, or offset+count is larger than arg.length.

Example:

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

public class MyClass {
  public static void main(String[] args) {
    boolean x1 = true;
    System.out.println(String.valueOf(x1)); 

    char x2 = 'Y';
    System.out.println(String.valueOf(x2));

    double x3 = 125.25;
    System.out.println(String.valueOf(x3));  
  }
}

The output of the above code will be:

true
Y
125.25

Example:

In the below example, valueOf() method is used with character array and object to return its string representation.

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

The output of the above code will be:

Hello
ell
HELLO

❮ Java String Methods