Java String valueOf() Method
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Java’s valueOf() method is a public static method in the String class. It returns the string representation of the argument passed to it.
The main purpose of this method is to convert different types of values, such as integer, float, boolean, object, etc., into human-readable string representations. This enables us to output these values in a readable manner.
Many overloaded variants of the valueOf() method are available to handle the conversion of different data types, such as int, float, char, boolean, etc.
Syntax of Java valueOf() Method
Here are some of the commonly used variants of the Java valueOf() method:
- static String valueOf(int num)
- static String valueOf(float num)
- static String valueOf(boolean sta)
- static String valueOf(double num)
- static String valueOf(char[] data, int offset, int count)
- static String valueOf(long num)
- static String valueOf(Object ob)
- static String valueOf(char chars[])
These methods accept arguments of the specified data type and convert them into string representations.
Return Values of valueOf() method in Java
The return value of the valueOf() method depends on the argument passed to it:
- valueOf(int iNum) – Returns the string representation of int iNum.
- valueOf(boolean sta) – Returns the string representation of the boolean argument.
- valueOf(float fNum) – Returns the string representation of the float fNum.
- valueOf(char[] data, 0, 15) – Returns the string representation of a specific subarray of the charArray argument from index 0 to 15.
- valueOf(char[] data, 0, 5) – Returns the string of charArray from index 0 to 5.
- valueOf(char[] data, 7, 9) – Returns the string of charArray starting at index 7 with a total count of 9.
- valueOf(Object ob) – Returns the string representation of the given object.
Internal Working of Java valueOf() Method
Here is a code snippet showing the internal working of the valueOf(Object obj) method:
public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }
As we can see, it checks if the passed object is null. If yes, it returns the string “null”. Otherwise, it calls the toString() method on the object to convert it to a string representation.
Examples of Java valueOf() Method
Example 1: valueOf() with integer and float
public class ValueOfExample { public static void main(String[] args) { int iNum = 30; float fNum = 4.56789f; String str1 = String.valueOf(iNum); String str2 = String.valueOf(iNum) + 3091; String str3 = String.valueOf(fNum); String str4 = String.valueOf(fNum) + 914.56789; System.out.println(str1); // Prints "30" System.out.println(str2); // Prints "303091" System.out.println(str3); // Prints "4.56789" System.out.println(str4); // Prints "4.5678914.56789" } }
Output:
30
303091
4.56789
4.5678914.56789
Example 2: valueOf() with char arrays
public class CharArrayToString { public static void main(String[] args) { char[] charArray = {'D', 'A', 'T', 'A', 'F', 'L', 'A', 'I', 'R'}; String str1 = String.valueOf(charArray); String str2 = String.valueOf(charArray, 0, 4); // Modify the start and length String str3 = String.valueOf(charArray, 4, 5); // Modify the start and length System.out.println(str1); // Prints "DATAFLAIR" System.out.println(str2); // Prints "DATA" System.out.println(str3); // Prints "FLAIR" } }
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
Output:
DATAFLAIR
DATA
FLAIR
Example 3: valueOf() with boolean
public class BooleanValueOfExample { public static void main(String[] args) { String str = "Hello, world!"; boolean sta = str.contains("Hello"); String str2 = String.valueOf(sta); System.out.println(str2); // Prints "true" } }
Output:
true
Conclusion
In conclusion, the valueOf() method in Java’s String class is a versatile tool for converting various data types into human-readable string representations. It provides multiple overloaded variants to handle integers, floats, booleans, characters, objects, and more. The return value of this method depends on the argument passed to it, ensuring flexibility in string conversion.
The internal working of the valueOf() method involves checking whether the input object is null and then calling the toString() method to create the string representation.
This method is an essential part of Java programming. It enables developers to easily transform different data types into strings, making data output more accessible and user-friendly. Understanding how to use valueOf() effectively can be valuable for Java developers.
Did you like this article? If Yes, please give DataFlair 5 Stars on Google