Open In App

Java String valueOf() Method

Last Updated : 07 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads