← Back to list

Difference between String.valueOf & int + “”

Internals of String s = String.valueOf(i);

Shaheen Biradar · 2026-03-22 08:47 · 100 claps · 0.9 min read paywalled
#string #string-theory #string-manipulation #string-methods #string-builder
Open on Medium ↗

Difference between String.valueOf & int + “”

Photo by Valentin Bolder on Unsplash

Photo by Valentin Bolder on Unsplash

Internals of String s = String.valueOf(i);

How it works

  • Calls a method in the String class.
  • Internally converts the integer using optimized logic.

Implementation

public static String valueOf(int i) {
    return Integer.toString(i);
}

public static String toString(int i) {
    if (i == Integer.MIN_VALUE)
        return "-2147483648";

    int size = stringSize(i);
    char[] buf = new char[size];
    getChars(i, size, buf);
    return new String(buf);
}

public static String getChars(int i, int size char[] buf){
  while (i >= 10) {
    int q = i / 10;
    int r = i % 10;
    buf[--pos] = (char) ('0' + r);
    i = q;
  }
  buf[--pos] = (char) ('0' + i);
  return new String(buf);
}

Why It’s Fast

String.valueOf(i) is efficient because:

  • ✅ No StringBuilder
  • ✅ No temporary strings
  • ✅ Uses pre-sized char[]
  • ✅ Minimal allocations

(2) Internals of int(i) + “”

new StringBuilder().append(i).append(“”).toString();

That means:

  • Create StringBuilder
  • Convert int inside append
  • Append empty string
  • Convert back to String

I used ChatGpt for my research. Hope this helps you in string conversion.

May Allah bless you, take care.


메타데이터
post_id
16c7b5ac8d99
slug
difference-between-string-valueof-int-16c7b5ac8d99
url
https://medium.com/@shaheen17995/difference-between-string-valueof-int-16c7b5ac8d99
canonical_url
https://medium.com/@shaheen17995/difference-between-string-valueof-int-16c7b5ac8d99
author_url
https://medium.com/@shaheen17995
status
ok
fetched_at
2026-07-18 16:53:00