In Java, valueOf() method converts data from its internal form into a human-readable form. It is a static method that is overloaded within a string for all of Java’s built-in types so that each type can be converted properly into a string.
It is called when a string representation of some other type of data is needed for example during a 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.
Syntax of valueOf()
The syntax of valueOf() function for different kind of inputs are:
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[])
Return Values of valueof()
It returns the string representation of the 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.
Internal Working of valueof() Method
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
Examples of Java valueOf() Method
Example 1:
Input : 30
// concatenating integer value with a String
Output: 3091
Input : 4.56589
// concatenating float value with a String
Output: 914.56589
Java
class ValueOfExa {
public static void main(String arg[])
{
int iNum = 30 ;
double fNum = 4.56789 ;
String s = "91" ;
String sample = String.valueOf(iNum);
System.out.println(sample);
System.out.println(sample + s);
sample = String.valueOf(fNum);
System.out.println(sample);
System.out.println(s + sample);
}
}
|
Output30
3091
4.56789
914.56789
Example 2:
Java
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;
sample = String.valueOf(data, 0 , 15 );
System.out.println(sample);
sample = String.valueOf(data, 0 , 5 );
System.out.println(sample);
sample = String.valueOf(data, 6 , 8 );
System.out.println(sample);
}
}
|
OutputGEEKS 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
Java
public class StringValueOfBoolean {
public static void main(String[] args)
{
String data = "Geeks for Geeks" ;
boolean bool = data.contains( "Geeks" );
System.out.println(String.valueOf(bool));
}
}
|
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.