Java String valueOf() Method Example

1. Introduction

The String.valueOf() method in Java is a static method used to convert different types of values into strings. This method is incredibly useful when you need to convert other primitive types or objects into strings for processing, logging, or concatenation. This tutorial will demonstrate how to use String.valueOf() with various data types.

Key Points

- valueOf() is used to convert primitives or objects to strings.

- It can handle char, int, long, float, double, and objects.

- The method returns a string representation of the passed argument.

2. Program Steps

1. Convert various data types to strings using String.valueOf().

2. Print the converted strings.

3. Code Program

public class StringValueOfExample {
    public static void main(String[] args) {
        // Convert different types to strings
        String intStr = String.valueOf(123);
        String boolStr = String.valueOf(true);
        String charStr = String.valueOf('Z');
        String longStr = String.valueOf(123456789012345L);
        String floatStr = String.valueOf(3.14f);
        String doubleStr = String.valueOf(1.61803);
        // Print the converted strings
        System.out.println("Integer to String: " + intStr);
        System.out.println("Boolean to String: " + boolStr);
        System.out.println("Char to String: " + charStr);
        System.out.println("Long to String: " + longStr);
        System.out.println("Float to String: " + floatStr);
        System.out.println("Double to String: " + doubleStr);


Integer to String: 123
Boolean to String: true
Char to String: Z
Long to String: 123456789012345
Float to String: 3.14
Double to String: 1.61803


1. String intStr = String.valueOf(123): Converts an integer to a string.

2. String boolStr = String.valueOf(true): Converts a boolean to a string.

3. String charStr = String.valueOf('Z'): Converts a character to a string.

4. String longStr = String.valueOf(123456789012345L): Converts a long to a string.

5. String floatStr = String.valueOf(3.14f): Converts a float to a string.

6. String doubleStr = String.valueOf(1.61803): Converts a double to a string.

7. Each System.out.println statement prints the result of converting different types to strings, showing the effectiveness of String.valueOf() in converting various data types.