E-Learning

Learn Java – How to Convert Int to String?

java-int-string

Learning how to convert an int value to String in Java is extremely easy. However, there are multiple ways to achieve the desired result, and this is where complexity comes in. Currently, there are three ways you can convert the int value to String. Let’s go through them one by one.

String.valueOf()

The best way to convert an int value to a String is to use the String.valueOf() function. The reasoning behind this is that int is a primitive value compared to Integer, which is a wrapper object. Remember, everything in Java is an object, which makes a difference in how the conversion should be handled.

If you want to convert an int to String, you need to use String.valueof(int). You can also use the function for other data types such as float, long, etc.

So, what does it look like? Let’s see it in action below.

Let’s see what goes inside the valueOf method.

As you can see, that it uses the .toString() method to do the conversion. This leads us to our second method of converting an int to string in Java.

Integer.toString()

The next method is toString(). It takes an integer as an argument and converts it to a string. Let’s see it in action below.

Output

The toString() method preserves the sign of negative numbers. However, it doesn’t do the same when it comes to handling positive numbers.

String Concatenation

String concatenation also works for converting an int value to String in Java. Even though you can do it, you should not use it in production-level code. This method is only shared for the sake of knowledge and should be used with caution.

All you need to do is add an empty string “” and a number using the + operator.

Do you think we covered all the methods for converting int to String? Comment below and let us know.

You can also check our website for videos about Java. Below are some examples:

  • Programming a Solar System (part 13) – Java

  • A new method how to learn create better webpage (part 1) – Java

You can also follow some of our broadcasters who program in Java as below:

  CallumC

  zzeorge:

Another cool way to find out interesting things about Java is to access our project page!

Avatar
About author

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.