How to concatenate int values in java?

Shamli picture Shamli · Apr 20, 2010 · Viewed 223.7k times · Source

I have the following values:

int a=1; 
int b=0;
int c=2;
int d=2;
int e=1;

How do i concatenate these values so that i end up with a String that is 10221; please note that multiplying a by 10000, b by 1000.....and e by 1 will not working since b=0 and therefore i will lose it when i add the values up.

Answer

Michael Borgwardt picture Michael Borgwardt · Apr 20, 2010

The easiest (but somewhat dirty) way:

String result = "" + a + b + c + d + e

Edit: I don't recommend this and agree with Jon's comment. Adding those extra empty strings is probably the best compromise between shortness and clarity.