Java int to String

In Java, you can convert an int to a String in several ways. Here are the most common methods:

1. String.valueOf()

The String.valueOf() method is the most commonly used approach for converting an int to a String. It converts any primitive type, including int, to its string representation.

Example:


public class IntToStringExample {
    public static void main(String[] args) {
        int num = 1234;
        
        // Convert int to String using String.valueOf()
        String str = String.valueOf(num);
        
        System.out.println("Converted String: " + str);
    }
}

Output: Converted String: 1234

Code Explanation:

String.valueOf(num) converts the integer num to a string.

2. Integer.toString()

The Integer.toString() method is another way to convert an int to a String. It is a method of the Integer class and provides the same result.

Example:


public class IntToStringExample {
    public static void main(String[] args) {
        int num = 567;
        
        // Convert int to String using Integer.toString()
        String str = Integer.toString(num);
        
        System.out.println("Converted String: " + str);
    }
}

Output: Converted String: 567

Code Explanation:

Integer.toString(num) converts the integer num to its string representation.

3. String Concatenation

In Java, you can convert an int to a String by simply concatenating the int with an empty string “”. This is a quick and simple method, but it is not always the best for performance in complex

Example:


public class IntToStringExample {
    public static void main(String[] args) {
        int num = 567;
        
        // Convert int to String using concatenation
        String str = num + "";
        
        System.out.println("Converted String: " + str);
    }
}

Output: Converted String: 567

Code Explanation:


num + "" converts the int to a String by concatenating the number with an empty string.

4. String.format()

You can also use String.format() to convert an int to a String. This method is often used when formatting numbers with specific patterns.

Example:


public class IntToStringExample {
    public static void main(String[] args) {
        int num = 200;
        
        // Convert int to String using String.format()
        String str = String.format("%d", num);
        
        System.out.println("Converted String: " + str);
    }
}

Output: Converted String: 200

Explanation:


String.format("%d", num) formats the integer num as a string. The %d is a format specifier for integers.

Java int to String – Objective Questions (MCQs)

Q1. Which of the following methods can convert an int to a String?






Q2. What will be the output of:

int num = 20;
String str = String.valueOf(num);
System.out.println(str + 5);






Q3. Which statement correctly converts an integer n to a string?






Q4. What will be the output of the following code?

int x = 50;
String s = "" + x;
System.out.println(s);






Q5. What is the return type of Integer.toString() method?






Related Java int to String Topics