Skip to content
Related Articles

Related Articles

Data Conversion Using valueOf() method in Java
  • Difficulty Level : Easy
  • Last Updated : 30 Mar, 2021

The valueOf() method converts data from its internal form into a 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 returns 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: 3091

Input : 4.56589
// concatenating  float value with a String 
Output: 914.56589 

Java




// 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
3091
4.56789
914.56789

Example 2: 

Java




// 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);
    }
}
Output
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

Java




// 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. 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :