Java - How to convert from Integer to String?

This is the minute role of information conversion tutorial, inward the first part y'all stimulate got learned how to convert String to Integer together with inward this article, y'all volition larn the contrary i.e. convert from Integer to String. Actually, y'all tin apply all the tricks, which I had told y'all before well-nigh converting long to String, together with autoboxing volition stimulate got assist of converting int to Integer inward Java. But, if y'all assist for functioning together with believe inward non using auto-boxing when non needed thus at that spot are nevertheless a duo of ways which straight converts an Integer object to String e.g. Integer.toString() method, which returns a String object together with doesn't stimulate got whatever auto-boxing overhead. Let's come across a duo of more ways to convert an Integer to String inward Java. Btw, if y'all are novel to Java together with simply started learning, I advise y'all to purchase the Head First Java, 2d Edition, i of the best books to larn Java.



3 ways to convert Integer to String inward Java

Following are iii ways to convert an Integer to String inward Java:
  1. Integer.toString()
  2. String concatenation
  3. String.format()



Solution 1 - Integer.toString()

This is the best together with straightforward agency to convert an Integer to String object inward Java. It doesn't require whatever auto-unboxing etc, but it volition throw NullPointerException if Integer is null every bit shown below:
Integer i = new Integer(10); String notNull = i.toString();  i = null; String nullString = i.toString(); // NPE
So, brand certain y'all exercise a nada depository fiscal establishment represent before calling toString() method on Integer object. Remember, nosotros are dealing amongst the object hither together with non primitive int, which tin never null.


Solution 2 - String concatenation

This is an indirect agency to telephone telephone toString() method of Integer flat but dissimilar previous method this i doesn't throw NullPointerException fifty-fifty if Integer is null, instead it volition exercise a String amongst literal value null, every bit shown below :

Integer i = new Integer(10); String sec = i.toString(); sec = "" + i; System.out.println(s);   i = null; sec = "" + i;  System.out.println(s);

Remember, String concatenation is replaced amongst StringBuilder append() call internally.


Solution 3 - String.format

This is the 3rd instance of converting an Integer to String inward Java, but dissimilar other ii this involves autoboxing. As shown inward the before article well-nigh converting int to String, y'all tin top an int primitive when y'all occupation %d every bit format instruction, together with this lawsuit inward String equivalent of integer passed every bit shown below.
Integer i = new Integer(3); String sec = String.format("%d", i); System.out.println(s); // 3
You tin see, hither nosotros bespeak to top an int variable but immediately since nosotros stimulate got an Integer object, it needs autoboxing.

Here is the squeamish summary of all iii examples of converting Integer to String inward Java:

 This is the minute role of information conversion tutorial Java - How to convert from Integer to String?


That's all well-nigh how to convert an Integer to String inward Java. As y'all stimulate got seen, the most straightforward agency is to occupation Integer.toString() method, it doesn't bespeak autoboxing together with it's withdraw agency to acquire String from Integer. Alternatively y'all tin concatenate Integer object amongst empty String. This volition internally telephone telephone the toString() only. The 3rd agency is simply for information purpose though it useful, but its purpose is different. You should occupation String.format() to create a formatted String non to convert an Integer to String.


Related Java Tutorials
If y'all similar this tutorial together with wants to larn to a greater extent than well-nigh how to convert i information type into another, Please depository fiscal establishment represent out next Java programming tutorials:
  • How to convert String to Double inward Java? (example)
  • How to convert char to String inward Java? (tutorial)
  • How to convert Enum to String inward Java? (example)
  • How to convert ByteBuffer to String inward Java? (program)
  • How to convert String to Date inward a thread-safe manner? (example)
  • How to convert float to String inward Java? (example)
  • How to convert byte array to String inward Java? (program)
  • How to convert double to Long inward Java? (program)
  • How to convert String to int inward Java? (example)
  • How to convert Decimal to Binary inward Java? (example)
  • How to parse String to long inward Java? (answer)


Further Reading
If y'all stimulate got simply started learning Java thus y'all bespeak a book. Head First Java is the best mass to start amongst but if y'all notice it lilliputian comic together with interested inward existent mass thus read Core Java ninth Edition past times Cay S. Horstman, the minute best mass to larn Java.

Subscribe to receive free email updates:

0 Response to "Java - How to convert from Integer to String?"

Posting Komentar