Data Conversion Using valueOf() method in Java
The valueOf() method converts data from its internal form into human-readable form.It is static method that is overloaded within string for all of Java’s build-in types, so that each type can be converted properly into a string.
It is called when a string representation of some other type data is needed-for example during concatenation operation.you can call this method with any data type and get a reasonable String representation
valueOf() returns java.lang.Integer, which is the object representative of the integer
Few forms of valueOf() :
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[])
Returns:
- It return string representation of given value
- valueOf(iNum); // Returns the string representation of int iNum.
- String.valueOf(sta); // Returns the string representation of the boolean argument.
- String.valueOf(fNum); // Returns the string representation of the float fnum.
- String.valueOf(data, 0, 15); // Returns the string representation of a specific subarray of the chararray argument.
- String.valueOf(data, 0, 5); // Returns the string of charArray 0 to 5
- String.valueOf(data, 7, 9); // Returns the string of charArray starting index 7 and total count from 7 is 9
Example-1
Input : 30 // concatenating integer value with a String Output : 9130 Input : 4.56589 // concatenating float value with a String Output : 914.56589
// Java program to demonstrate // working of valueOf() methods class ValueOfExa { public static void main(String arg[]) { int iNum = 30; double fNum = 4.56789; String s = "91"; // Returns the string representation of int iNum. String sample = String.valueOf(iNum); System.out.println(sample); // concatenating string with iNum System.out.println(sample + s); // Returns the string representation of the float fnum. sample = String.valueOf(fNum); System.out.println(sample); // concatenating string with fNum System.out.println(s + sample); } } |
Output:
30 9130 4.56789 914.56589
Example-2
// Java program to demonstrate // working of valueOf() methods class ValueOfExa { public static void main(String arg[]) { char[] data = { 'G', 'E', 'E', 'K', 'S', ' ', 'F', 'O', 'R', ' ', 'G', 'E', 'E', 'K', 'S' }; String sample; // Returns the string representation // of a specific subarray of the chararray argument sample = String.valueOf(data, 0, 15); System.out.println(sample); // Returns the string of charArray 0 to 5 sample = String.valueOf(data, 0, 5); System.out.println(sample); // Returns the string of charArray starting // index 6 and total count from 6 is 8 sample = String.valueOf(data, 6, 8); System.out.println(sample); } } |
GEEKS FOR GEEKS GEEKS FOR GEEK
Example-3
Input :Geeks for Geeks
// check if String value contains a
// specific string by method contains("Geeks");
Output :true
// The following example shows the // usage of <strong>valueOf(boolean sta)</strong method. public class StringValueOfBoolean { public static void main(String[] args) { // declare a String String data = "Geeks for Geeks"; // check if String value contains a specific string boolean bool = data.contains("Geeks"); // print the string equivalent of our boolean check System.out.println(String.valueOf(bool)); } } |
Output:
true
Difference between parseInt and valueOf in java
The API for Integer.valueOf(String) does indeed say that the String is interpreted exactly as if it were given to Integer.parseInt(String). However, valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int.
Recommended Posts:
- BigInteger valueOf() Method in Java
- Integer valueOf() Method in Java
- BigDecimal valueOf() Method in Java
- TimeUnit valueOf() method in Java with Examples
- DayOfWeek valueOf() method in Java with Examples
- Locale.Category valueOf() method in Java with Examples
- Locale.FilteringMode valueOf() method in Java with Examples
- Java.lang.Long.valueOf() method with examples
- Character.valueOf() in Java with examples
- Base conversion in Java
- Conversion of Array To ArrayList in Java
- Conversion of Java Maps to List
- Widening Primitive Conversion in Java
- Type conversion in Java with Examples
- Array to ArrayList Conversion in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



