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?
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?
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.
public static void main(String[] args) {
int A = 5;
String str = String.format("%s%s", A, A);
System.out.println(str);
}