How to print 2 int(s) but not add them?

F.A picture F.A · Jan 12, 2015 · Viewed 43.6k times · Source

I know this seems like a stupid question (so excuse me). Basically this is what I want to do:

int a = 5;
int b = 3;

System.out.print(a+b);

this will give me 8, but is there a way other than putting an empty string inbetween for it to print 5 and 3 (and not by converting the int to a string)?

Thank you so much.

Answer

Alan picture Alan · Jan 12, 2015

The print method will simply convert its argument to a string and write out the result. Therefore, if you want to display the two numbers concatenated, you will need to do this yourself by converting them to a string (either explicitly, or using "" as you've already mentioned).

If you want to avoid building the string yourself, you'd probably need to use the printf() method:

System.out.printf("%d%d", a, b);