Best way to cutting and concatenation integer operations

Viewed 45

What is the best way to cut an integer variable from another integer variable or concatenate two integer variables?

I mean, in the performance way? - I consider two options:

  • cast an int to a String and perform a simple substring/concatenation operation and cast the String back to an int
  • or perform an algorithmic operation, this way:
    public static Integer concatenateInt(int a, int b) {
        if(b < 0){
            b *= -1;
        }
        int proxyB = b/10;
        a *= 10;
        for (; proxyB > 0; proxyB /= 10) {
            a *= 10;
        }

        if(a < 0){
            a -= b;
        } else {
            a += b;
        }
        return a;
    }

  • via String:
        return Integer.parseInt(a + "" + b);

The cutting of integer values will be a more complicated algorithm - so which way is preferred for best performance?

0 Answers
Related