How to Concat Int to String in Java

How to Concat Int to String in Java? Here in this blog, we will see how to concat string type and an integer type. Even though they both are different data types we still concat them.

For example:-
String = “ABC”
int = 123
After concatenation:- “ABC123”
Our aim is to yield the above result. See the below code we have used different ways to concatenate int to string in Java.

Concat Int to String in Java using “+” Operator

The “+” operator is known as the addition operator but when it comes to strings then Java supports operator overloading only for the “+” operator. For string, “+” behaves as a concatenation operator. Whenever “+” encounters a string after that any value-added will become a string.

public class Main {
   public static void main(String[] args) {
      String string = "KnowProgram";
      int num = 123;
      String str = string + num;
      System.out.println(str);
   }
}

Output:-

KnowProgram123

public class Main{
   public static void main(String[] args) {
      int num1 = 1;
      int num2 = 2;
      int num3 = 3;

      String str = "" + num1 + num2 + num3;
      System.out.println(str);
   }
}

Output:-

123

Since we are adding an empty string before adding the numbers, therefore, remaining addition operation after the string will become part of the concatenation operation but if we write:- String str = num1 + num2 + num3 + ""; then we will get the result = 6. Whereas String str = num1 + num2 + "" + num3; will give result 33 because 1+2 + "" + “3” = “33”

We can also convert the first number to a string to add numbers as a string type. Below example demonstrating it:-

public class Main{
   public static void main(String[] args) {
      int num1 = 1;
      int num2 = 2;
      int num3 = 3;

      String str = String.valueOf(num1) + num2 + num3;
      System.out.println(str);
   }
}

Output:-

123

Concat Int to String using format() method

The string class contains the format() method and using it we can concat int to string type and it will return a string value. Let us see it through an example:-

public class Main {
   public static void main(String[] args) {
      String string = "KnowProgram";
      int num = 123;
      String str = String.format("%s%s", string, num);
      System.out.println(str);
   }
}

Output:-

KnowProgram123

public class Main {
   public static void main(String[] args) {
      int num1 = 1;
      int num2 = 2;
      int num3 = 3;

      String str = String.format("%s%s%s", num1, num2, num3);
      System.out.println(str);
   }
}

Output:-

123

Concat Int to String Using StringBuilder

We can create an object of the StringBuilder class and add string and int to the StringBuilder and later we will convert StringBuilder to a String type value using the toString() method. Let us demonstrate it through the code:-

public class Main {
   public static void main(String[] args) {
      String string = "KnowProgram";
      int num1 = 1;
      int num2 = 2;
      int num3 = 3;

      StringBuilder sb = new StringBuilder();
      sb.append(string);
      sb.append(num1);
      sb.append(num2);
      sb.append(num3);

      String str = sb.toString();
      System.out.println(str);
   }
}

Output:-

KnowProgram123

The same code also can be written as follows:-

public class Main {
   public static void main(String[] args) {
      String string = "KnowProgram";
      int num1 = 1;
      int num2 = 2;
      int num3 = 3;

      String str = 
            new StringBuilder()
            .append(string)
            .append(num1)
            .append(num2)
            .append(num3)
            .toString();
      System.out.println(str);
   }
}

Output:-

KnowProgram123

Concat Int to String using concat() method

The string class contains the concat() method and using it we can concat int to string type and it will return a string value. Let us see it through an example:-

public class Main {
   public static void main(String[] args) {
      String string = "KnowProgram";
      int num = 123;
      String str = string.concat(String.valueOf(num));
      System.out.println(str);
   }
}

Output:-

KnowProgram123

Concat Int to String Using Stream API

import java.util.Arrays;
import java.util.stream.Collectors;

public class Main {
   public static String concatenate(int... num) {
      return Arrays.stream(num)
                 .mapToObj(String::valueOf)
                 .collect(Collectors.joining());
   }

   public static void main(String[] args) {
      int num1 = 1;
      int num2 = 2;
      int num3 = 3;

      String str = concatenate(num1, num2, num3);
      System.out.println(str);
   }
}

Output:-

123

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *