devxlogo

Integer-to-string Conversions

Integer-to-string Conversions

Question:
I want to convert an integer into a string. Is there anything in Java similar to the itoa() function in C?

Answer:
To convert a number (or in fact, any Java object) into a string isvery easy. Just concatenate it with the empty string.

     int n = 5;     String s = n + "";

You can also call the toString() function that is defined for allobjects, but that is a pain for numbers. A number isn’t an object, soyou first have to make the number an object.

     Integer i = new Integer(n);     String s = i.toString();

You can do it all in one statement:

     String s = new Integer(n).toString();

Clearly the first method, of concatenating with “”, seems moreattractive.

See also  How IoT Tech Will Transform Logistics
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist