Open In App

Java String valueOf() Method

Last Updated : 02 Dec, 2024
Comments
Improve
Suggest changes
13 Likes
Like
Report

The valueOf() method of the String class in Java helps to convert various data types like integers, floats, booleans, and objects into their string representations. It makes it simple to work with string manipulation, logging, and displaying data efficiently.

Example:

To convert a number into text for easy display or use in-text operations, we can use the valueOf() method.


Output
String representation: 21

Explanation: The number 21 is converted to the string "21" for easier use in text-related tasks.

Syntax of valueOf() Method

The syntax of valueOf() method for different kind of inputs are:

public static String valueOf(int i)

public static String valueOf(long l)

public static String valueOf(float f)

public static String valueOf(double d)

public static String valueOf(boolean b)

public static String valueOf(char c)

public static String valueOf(char[] data)

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

public static String valueOf(Object obj)

Return Type: It returns a String that represents the input data. If the input is null, it returns the text "null".

Other Examples of String valueOf() Method

1. Convert Boolean to String Using valueOf()

Example:


Output
true

Explanation: In the above example, the boolean true becomes the string "true" for display or text use.

2. Convert Object to String Using valueOf()

Example:


Output
null

Explanation: In the above example, the method converts the null object into the text "null" without errors.

3. Convert Part of a Char Array to String Using valueOf()

Example:


Output
Hello

Explanation: In the above example, the first five characters ('H', 'e', 'l', 'l', 'o') are converted into the string "Hello".



Next Article
Article Tags :
Practice Tags :

Similar Reads