String valueOf example in java

The Java String class defines following methods to convert various Java primitives to Java String object.

1) static String valueOf(int i) Converts argument int to String and returns new String object representing argument int.

2) static String valueOf(float f) Converts argument float to String and returns new String object representing argument float.

3) static String valueOf(long l) Converts argument long to String and returns new String object representing argument long.

4) static String valueOf(double i) Converts argument double to String and returns new String object representing argument double.

5) static String valueOf(char c) Converts argument char to String and returns new String object representing argument char.

6) static String valueOf(boolean b) Converts argument boolean to String and returns new String object representing argument boolean.

7) static String valueOf(Object o) Converts argument Object to String and returns new String object representing argument Object.

package com.java.connect.string;
 
// String valueOf example in java
public class StringValueOfExample {
 
    public static void main(String[] args) {
        // Decalre an int
        int i = 100;
        // Decalre a float
        float f = 12.5f;
        // Decalre a long
        long l = 27367623l;
        // Decalre a double
        double d = 1098.0d;
        // Decalre a char
        char c = 'g';
        // Decalre a boolean
        boolean b = true;
        // Decalre an Object instance
        Object object = new String("Some string");
 
        // Convert int to String
        System.out.println(String.valueOf(i));
        // Convert float to String
        System.out.println(String.valueOf(f));
        // Convert long to String
        System.out.println(String.valueOf(l));
        // Convert double to String
        System.out.println(String.valueOf(d));
        // Convert char to String
        System.out.println(String.valueOf(c));
        // Convert boolean to String
        System.out.println(String.valueOf(b));
        // Convert Object to String
        System.out.println(String.valueOf(object));
    }
}

The output of above example will be as:

100
12.5
27367623
1098.0
g
true
Some string