In this section, we will learn what the String valueOf() method is and how to use it in Java.
What is Java String valueOf() Method?
The `valueOf()` method is used to get the String representation of other data types.
For example, we have a value of type `int` and we want to store it in a variable of type `String`, then this method is the way to go.
Note: The method is static and so we need to use the name of the class to access these methods.
Java valueOf() Method Syntax:
static String valueOf(boolean b) static String valueOf(char c) static String valueOf(char[] data) static String valueOf(char[] data, int offset, int count) static String valueOf(double d) static String valueOf(float f) static String valueOf(int i) static String valueOf(long l) static String valueOf(Object obj)
valueOf() Method Parameters:
This method takes one argument, and that is the value that we want to get its string representation.
valueOf() Method Return Value:
The return value is the String representation of the value we set as the argument of this method.
valueOf() Method Exceptions:
This method does not throw an exception.
Example: using String valueOf() method
public class Simple { public static void main(String[] args) { int i = 10; long l = 30; boolean b = true; double d= 33.3; char[] chars = {'H','e','l','l','o'}; System.out.println(String.valueOf(i)); System.out.println(String.valueOf(l)); System.out.println(String.valueOf(b)); System.out.println(String.valueOf(d)); System.out.println(String.valueOf(chars)); } }
Output:
10 30 true 33.3 Hello