![]() |
VOOZH | about |
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.
String representation: 21
Explanation: The number 21 is converted to the string "21" for easier use in text-related tasks.
Table of Content
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".
Example:
true
Explanation: In the above example, the booleantrue becomes the string "true" for display or text use.
Example:
null
Explanation: In the above example, the method converts the nullobject into the text "null" without errors.
Example:
Hello
Explanation: In the above example, the first five characters ('H', 'e', 'l', 'l', 'o') are converted into the string "Hello".