How to use valueOf() method of String Class In Java

The String class has a static method valueOf() that can be used to convert the Integer to String as shown below:

Below is the implementation of the above method:

Java




// Java Program to Illustrate Integer to String Conversions
// Using valueOf() Method of String class
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Custom integer input
        int c = 1234;
  
        // Converting above integer to string
        // using valueOf() Method
        String str3 = String.valueOf(c);
  
        // Printing the integer stored in above string
        System.out.println("String str3 = " + str3);
    }
}


Output

String str3 = 1234

Java Convert int to String | Different Ways Of Conversion

Converting Integers to Strings involves using the Integer classes toString() or String.valueOf() for direct conversion. String.format() is another method offering flexible formatting options. Using StringBuilder or StringBuffer for appending integer values as strings is efficient for extensive string manipulation.

We generally counter with such conversion articles because many operations can be performed over a string while we are limited to when it comes to integers. We have a wide varied list of in-built methods in the String class that helps us perform hassle-free operations. 

Suppose we are required to concatenate two integers then it would become a tedious job as we need to go through as we need to deal with the number system corresponding to which we will be playing mathematics within the number system. But in order to convert integers to strings in Java, we have some inbuilt methods and classes which make our work too easy. 

Tip: We generally convert primitive class data members types though we have the concept of Wrapper classes to Strings because in practical programming in java we deal with strings.

Similar Reads

How to convert int to string in Java?

There are certain methods for Integer to String conversions are mentioned below:...

Using toString Method of Integer Class

The Integer class has a static method toString() that returns a String object representing the specified int parameter. The argument is converted and returned as a string instance. If the number is negative, the sign will be preserved....

Using valueOf() method of String Class

...

Using toString() method of the Integer class

The String class has a static method valueOf() that can be used to convert the Integer to String as shown below:...

Using concatenation with an empty string

...

Advanced Methods to Convert int to String Java

It is different from method 1 as proposed above, as in this method we use an instance of the Integer class to invoke its toString() method....

Using DecimalFormat Class

...

Using StringBuffer class

Approach: Here we will declare an empty string and using the ‘+’ operator, we will simply store the resultant as a string. Now by this, we are successfully able to append and concatenate these strings....

Using StringBuilder Class

...

Summary

There are certain advance Methods are mentioned below:...