Sanjeev Mansotra - How to add the two or more numbers in to string in java?

Hello @sanjeevmansotra! It sounds like you want to concatenate two or more numbers and convert the result to a string in Java. You can achieve this using the + operator for concatenation and then using String.valueOf() or Integer.toString() to convert the result to a string for instance:

public class NumberToStringConcatenation {
    public static void main(String[] args) {
        int num1 = 5;
        int num2 = 10;

        // Using + operator for concatenation and String.valueOf()
        String result = String.valueOf(num1) + String.valueOf(num2);

        // Alternatively, you can directly use the + operator with numbers
        // This will implicitly convert the numbers to strings
        // String result = num1 + num2;

        System.out.println("Result: " + result);
    }
}

Just a brief explanation of the above code: the String.valueOf() method is used to convert each number to its string representation, and then the + operator is used to concatenate the strings.

Alternatively, you can directly use the + operator with the numbers, and Java will automatically convert them to strings.

1 Like