Java String valueOf()The String.valueOf() method in Java is a multipurpose static method. Its major function lies in the conversion of types of data, such as primitive types and objects, into strings. The technique provides an efficient and convenient way to construct string objects from different sources. Java's valueOf() method is overloaded with numerous forms, each of that is designed to handle specific data types. It can handle converting boolean, char, char array, double, float, int, long, and Object types into their respective string representations. With the help of valueOf(), developers can get back string representations of variables and objects without the need to concatenate or modify strings directly. It simplifies the code and enhances readability by offering a cleaner option instead of string conversion with the hands. Internal implementationMethod SignatureThe signature or syntax of string valueOf() method is given below: ReturnsString representation of a given value. Java String valueOf() Method ExampleStringValueOfExample.java Output: 3010 Java String valueOf(boolean bol) Method ExampleThis is a boolean version of overloaded valueOf() method. It takes boolean value and returns a string. Let's see an example. StringValueOfExample.java Output: The boolean value is: true String representation using valueOf: true Java String valueOf(char ch) Method ExampleThis is a char version of overloaded valueOf() method. It takes char value and returns a string. Let's see an example. StringValueOfExample.java Output: The char value is: A String representation using valueOf: A Java String valueOf(float f) and valueOf(double d) MethodIt is a float version of the overloaded valueOf() method. It takes a float value and returns a string. Let's see an example. StringValueOfExample.java Test it NowOutput: Float value and its String representation: 123.45 -> 123.45 Double value and its String representation: 987.654 -> 987.654 Java String valueOf() Complete ExamplesLet's see an example where we are converting all primitives and objects into strings. StringValueOfExample.java Output: Boolean: true Char: A Int: 42 Long: 123456789 Float: 123.45 Double: 987.654 Integer Object: 123 Double Object: 456.789 Next TopicJava-integer-getinteger-method |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India