Java Program to convert int type variables to String

Updated on November 25, 2024
Convert int type variables to string header image

Introduction

Java developers often need to convert data types from one form to another to meet different programming requirements, such as combining numerical values with text. Converting int type variables to String is a frequent necessity, especially when you need to output numbers in textual format or append them to strings for various operations like logging, displaying, or processing in functions that accept strings only.

In this article, you will learn how to convert int type variables to String in Java through several methods and examples. Harness these techniques to handle integers and strings efficiently in your Java applications.

Using the Integer.toString() Method

Convert an Integer to String Using Integer.toString()

  1. Declare an integer variable.

  2. Use the Integer.toString() method to convert it to a string.

    java
    int number = 123;
    String numberStr = Integer.toString(number);
    System.out.println(numberStr);
    

    This example takes an integer number and uses Integer.toString() to convert it into a string numberStr. The output will be the string representation of the integer.

Using the String.valueOf() Method

Convert with String.valueOf()

  1. Start with an integer.

  2. Apply the String.valueOf() method to perform the conversion.

    java
    int number = 250;
    String numberStr = String.valueOf(number);
    System.out.println(numberStr);
    

    In this snippet, String.valueOf() converts the integer number to a string numberStr. It serves a similar purpose to Integer.toString() but is often seen as more versatile for different data types.

Using the Concatenation Operator

Simple Concatenation to Convert Integer to String

  1. Define an integer value.

  2. Concatenate it with an empty string to get a string result.

    java
    int number = 42;
    String numberStr = number + "";
    System.out.println(numberStr);
    

    By concatenating number with an empty string, Java automatically converts the integer to a string. This method is straightforward and often used for its simplicity.

Using Java 8 Streams for Bulk Conversion

Convert an Array of Ints to Strings

  1. Create an array of integers.

  2. Convert the entire array to an array of strings using Java Streams.

    java
    int[] numbers = {10, 20, 30, 40};
    String[] stringNumbers = Arrays.stream(numbers)
                                  .mapToObj(String::valueOf)
                                  .toArray(String[]::new);
    
    for (String s : stringNumbers) {
        System.out.println(s);
    }
    

    This example demonstrates how to use a stream to convert each integer in an array numbers into strings. The mapToObj(String::valueOf) method applies String.valueOf() to each element, converting them into a stream of strings, which is then collected into a new string array stringNumbers.

Conclusion

Converting integers to strings in Java can be accomplished through multiple methods, each suitable for different contexts within Java development. Whether using Integer.toString(), String.valueOf(), concatenation, or handling multiple conversions using streams, each method provides the flexibility to handle integer-to-string conversion effectively. By understanding and applying these techniques, you ensure seamless type handling and data formatting in your Java programs.