4 ways to convert int value to String inwards Java? Examples

You tin work String.valueOf() or Integer.toString() method to convert an int value to String object inwards Java. Remember, String is non precisely a char array but a amount characteristic object inwards Java. The criterion way to convert ane object to String inwards Java is precisely calling the toString() method, but since hither nosotros demand to convert primitive int to String, I prefer to work String.valueOf(int) method. It reads improve because nosotros are converting an int to String too non an Integer to String. There is a divergence betwixt them, int is a primitive value spell Integer is a wrapper object inwards Java. BTW, at that topographic point is ane to a greater extent than way to convert an int value to String inwards Java, the easiest one, String concatenation. Even though Java doesn't back upward operator overloading, + operator behaves differently amongst integer too String operands. If ane of the declaration to this binary operator is String too then it performs String concatenation, otherwise, it does addition. So you lot tin convert an int value e.g. 10 to a String value "10" past times precisely doing "" + 10.

Btw, I don't recommend that equally preferred way because it looks similar code odour too at that topographic point is a direct chances of mistake if at that topographic point is to a greater extent than declaration e.g. 10 + 10 + "" volition arrive at "20" too non "1010" because + is correct associative operator too it volition start perform improver of 10 + 10 too and then String concatenation betwixt 20 + "".




4 Ways to Convert an int value to a String inwards Java


Solution 1 - String.valueOf() 

It's no dubiousness my favorite way for int to String conversion because you lot tin work the same approach to convert other information types to String equally good e.g. you lot tin telephone scream upward String.valueOf(long) to convert a long to String, String.valueOf(double) to convert a double value to String too String.valueOf(float) to convert a float value to String inwards Java.

Most importantly It reads well. It's too a expert instance of the static mill method, which provides roughly pregnant advantages over constructor for creating an object. Btw, Internally valueOf() too calls the Integer.toString() method for conversion, equally shown below:

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

Now let's meet how to work this method, hither is an instance of using String.valueOf() for int to String conversion inwards Java:

String hundred = String.valueOf(100); // you lot tin overstep int constant  int x = 10; String x = String.valueOf(ten) // You tin too overstep int variable


Solution 2 - Integer.toString()

Another way to convert an int value to String is past times using Integer.toString() method. This method converts the passed declaration into int primitive equally shown below:

String 2 = Integer.toString(20); // returns "20" String ane = Integer.toString(01); // returns "1" String twentyOne = Integer.toString(-21); // returns -21 String 7 = Integer.toString(+7); // returns 7

You tin overstep this method a signed int value but it volition save sign solely for the negative number. Even if you lot overstep +7, it volition exercise String equally "7" without plus sign.

Integer.toString() past times default converts int to String inwards decimal format i.e. base of operations 10, if you lot desire to convert int to String inwards whatever other number organisation e.g. binary, octal or hexadecimal you tin work overloaded version of Integer.toString(int value, int radix) which too accepts a radix. You tin overstep radix equally xvi to convert int to hexadecimal String, 8 to convert int value to Octal String too 2 to convert int value to binary String equally shown inwards the next example:

String hexa = Integer.toString(20,16); // "14" String ocatal = Integer.toString(20, 8); // "24" String binary = Integer.toString(20, 2); // "10100"

So, if you lot desire to convert int to String inwards the unlike number system, you lot tin work Integer.toString() for that purpose.


Solution 3 - String concatenation

This is the easiest way to convert an int primitive to String object inwards Java. All you lot demand to exercise is concatenate the int value amongst an empty String e.g. "" + 100 too this volition render "100", no error, no issue, unproblematic too easy, but I don't similar that inwards production code? Why? isn't it most readable equally well? yes, it does but somehow it looks similar a hack to me than a proper solution. roughly other argue is that it internally uses StringBuilder for concatenation, which way ane to a greater extent than temporary object for GC to cleanup. Nevertheless, if you lot similar you lot tin work this approach, specially during testing.

String G = "" + 1000; String v = "" + 5;


Solution four - String.format() method

The format() method was introduced inwards java.lang.String class on JDK 1.5 too its primary work of zero but to format numbers int to String. You tin work too work this method to precisely convert an int value to String equally shown below:

String 1000000 = String.format("%d", 1000000)

It's specially useful for converting large int values to String. For to a greater extent than examples of formatting String inwards Java, meet here.

convert an int value to String object inwards Java four ways to convert int value to String inwards Java? Examples

Java Program to convert int to String inwards Java

Here is our sample Java plan to convert an int primitive to String object. In this example, you lot volition larn all 3 ways too ane bonus way.

/**  * Java Program to convert an int value to String object. In this article, nosotros  * volition bring a await at four unlike ways of converting int to String inwards Java. i)  * String.valueOf() ii) Integer.toString() iii) String concatenation iv) String  * format() method  *  * @author WINDOWS 8  */ public class Int2String {      public static void main(String args[]) {          // 1st Example - past times using String.valueOf() method         String hundred = String.valueOf(100); // you lot tin overstep int constant         int x = 10;         String str = String.valueOf(ten); // You tin too overstep int variable          // 2d Example - past times using Integer.toString()         String 2 = Integer.toString(20); // returns "20"         String ane = Integer.toString(01); // returns "1"         String twentyOne = Integer.toString(-21); // returns -21         String 7 = Integer.toString(+7); // returns 7          // tertiary Example - past times using String concatenation         String G = "" + 1000;         String v = "" + 5;          // quaternary Example - past times using String format() method         String 1000000 = String.format("%d", 1000000);      }  } 


That's all near how to convert an int value to String inwards Java. As I said, you lot tin work either Integer.toString(), String.valueOf() or precisely concatenation amongst empty String to exercise this task. Though it's improve to work String.valueOf() method, it's to a greater extent than readable too too the criterion way to convert whatever primitive information type to String inwards Java. You tin too work String.format() to acquire your int value equally nicely formatted String, specially suitable to convert large int values e.g. 1,000,000

Subscribe to receive free email updates:

0 Response to "4 ways to convert int value to String inwards Java? Examples"

Posting Komentar