1
public class Main
{
    public static void main(String[] args) {

        int A=5 ;
        String str = "A"+"A";
        System.out.println(str);

    }
}

The output is : AA

but i want the output as: 55 can any one suggest how to do?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841

2 Answers2

2

You can leverage implicit coercion to string from int when you join the int variables with an empty String :

int A=5 ;
String str = A + "" + A;
System.out.println(str);

Just remove the quotes from A and append the variable A with an empty string that will prompt the Java compiler to translate the expression to String.valueOf(A) + "" + String.valueOf(A) implicitly and convert it to string.

Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
0

How to format strings in Java

  public static void main(String[] args) {
    int A = 5;
    String str = String.format("%s%s", A, A);
    System.out.println(str);
  }
arcadeblast77
  • 560
  • 2
  • 12