Sunday, November 24, 2019

4 Ways To Convert Int Value To String Inwards Java? Examples

You tin utilisation String.valueOf() or Integer.toString() method to convert an int value to String object inward Java. Remember, String is non exactly a char array but a amount characteristic object inward Java. The measure way to convert i object to String inward Java is exactly calling the toString() method, but since hither nosotros demand to convert primitive int to String, I prefer to utilisation String.valueOf(int) method. It reads amend because nosotros are converting an int to String together with non an Integer to String. There is a deviation betwixt them, int is a primitive value piece Integer is a wrapper object inward Java. BTW, at that spot is i to a greater extent than way to convert an int value to String inward Java, the easiest one, String concatenation. Even though Java doesn't back upwards operator overloading, + operator behaves differently amongst integer together with String operands. If i of the declaration to this binary operator is String thence 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 exactly doing "" + 10.

Btw, I don't recommend that every bit preferred way because it looks similar code aroma together with at that spot is a run a endangerment of fault if at that spot is to a greater extent than declaration e.g. 10 + 10 + "" volition attain "20" together with non "1010" because + is correct associative operator together with it volition origin perform add-on of 10 + 10 together with thence String concatenation betwixt 20 + "".




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




Solution 1 - String.valueOf() 

It's no incertitude my favorite way for int to String conversion because you lot tin utilisation the same approach to convert other information types to String every bit good e.g. you lot tin telephone phone String.valueOf(long) to convert a long to String, String.valueOf(double) to convert a double value to String together with String.valueOf(float) to convert a float value to String inward Java.

Most importantly It reads well. It's likewise a skilful instance of the static manufactory method, which provides around meaning advantages over constructor for creating an object. Btw, Internally valueOf() likewise calls the Integer.toString() method for conversion, every bit shown below:

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

Now let's come across how to utilisation this method, hither is an instance of using String.valueOf() for int to String conversion inward Java:

String hundred = String.valueOf(100); // you lot tin exceed int constant  int x = 10; String x = String.valueOf(ten) // You tin likewise exceed 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 every bit shown below:

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

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

Integer.toString() past times default converts int to String inward decimal format i.e. base of operations 10, if you lot desire to convert int to String inward whatsoever other number organization e.g. binary, octal or hexadecimal you tin utilisation overloaded version of Integer.toString(int value, int radix) which likewise accepts a radix. You tin exceed radix every bit xvi to convert int to hexadecimal String, 8 to convert int value to Octal String together with 2 to convert int value to binary String every bit shown inward 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 inward the dissimilar number system, you lot tin utilisation Integer.toString() for that purpose.


Solution 3 - String concatenation

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

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


Solution four - String.format() method

The format() method was introduced inward java.lang.String class on JDK 1.5 together with its primary utilisation of nil but to format numbers int to String. You tin utilisation likewise utilisation this method to exactly convert an int value to String every bit shown below:

String meg = 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 inward Java, come across here.

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

Java Program to convert int to String inward 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 together with i bonus way.

/**  * Java Program to convert an int value to String object. In this article, nosotros  * volition bring a await at four dissimilar ways of converting int to String inward 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 exceed int constant         int x = 10;         String str = String.valueOf(ten); // You tin likewise exceed int variable          // s Example - past times using Integer.toString()         String 2 = Integer.toString(20); // returns "20"         String i = Integer.toString(01); // returns "1"         String twentyOne = Integer.toString(-21); // returns -21         String 7 = Integer.toString(+7); // returns 7          // third Example - past times using String concatenation         String G = "" + 1000;         String 5 = "" + 5;          // quaternary Example - past times using String format() method         String meg = String.format("%d", 1000000);      }  } 


That's all nearly how to convert an int value to String inward Java. As I said, you lot tin utilisation either Integer.toString(), String.valueOf() or exactly concatenation amongst empty String to exercise this task. Though it's amend to utilisation String.valueOf() method, it's to a greater extent than readable together with likewise the measure way to convert whatsoever primitive information type to String inward Java. You tin likewise utilisation String.format() to acquire your int value every bit nicely formatted String, specially suitable to convert large int values e.g. 1,000,000


Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

No comments:

Post a Comment