Converting character array to string

You can use a string for various tasks that an integer cannot. For instance, concatenation, identifying a substring, and so on. Let’s say we wish to concatenate two integers or see if an integer has a digit; if we used numerical operations, this would be a pain. Converting a number to a string, on the other hand, makes things easier.

In Java, we can use the String.valueOf() and Integer.toString() methods to convert int to String. We can also utilize the String.format() method, the string concatenation operator, and other methods.

Scenario

Because everything in the form is presented as a string, it’s usually employed when we need to display a number in a text field. It is necessary to create a Java program to convert an integer to a String in various scenarios when developing any application or website.

Consider a scenario in our Java program where the result value received after executing certain arithmetic operations on int variables is an integer value. This value, however, must be supplied to a web page text field or text area field. It would help if you first convert the int value to a String in such circumstances.

Converting an integer to a string in Java

In this article, we’ll look at the many ways for converting Integer to String in Java and some fun programming examples. In addition, we’ll look at how to convert Int to String in Java using the following methods given by various Java classes:

  • String concatenation
  • String.valueOf()
  • String.format()
  • Integer.toString()
  • Integer.String(int)
  • StringBuilder append ()
  • StringBuffer append ()
  • DecimalFormat format ()

Let’s start by examining the first method and drilling down through the list until we are done!

String.valueOf()

It is a function that returns the value of a string – Int to String is converted using the String.valueOf() function. The String class has a static function called valueOf(). The valueOf() method’s signature is as follows:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public static String valueOf(int i)</pre>
</div>

String.valueOf() is used to convert an int to a string in Java. This method will return a string if you pass it an integer (as an int or an Integer) like:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>String.valueOf(Integer(512));</pre>
</div>

The valueOf() overloading methods of the String class are static. These overloading methods transform parameters of primitive data types such as int, long, and float to String data types.

Let’s look at some simple Java code to convert an int to a String. Here’s an example program to see how to use the String.valueOf() method. We’ll convert the integer intVal to a String using the String.valueOf() method in this application.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>int intVal=10;  
String stringVal=String.valueOf(intVal);//Now it will return "10"</pre>
</div>

Let’s look at a simple java example of converting String to int.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public class IntToStringCode{  
  public static void main(String args[]){  
    int intVal=125;  
    String stringVal=String.valueOf(intVal);  

    System.out.println(intVal+500);//625 because + is binary plus operator  
    System.out.println(stringVal+500);//125500 because + is string concatenation operator  
  }
}  </pre>
</div>

Use the getClass().getName() method to get the type of a variable.

Example : String.valueOf()

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public class main{
  public static void main(String[] args){
    
    Integer intVal = new Integer(512);

    System.out.println("Results before the  conversion: " + intVal.getClass().getName());
    
    System.out.println("Results after the conversion: " + String.valueOf(intVal).getClass().getName());
  }
}</pre>
</div>

Example: Conversion using String.valueOf(int)

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	int intVal= 5012;
	String stringVal = String.valueOf(intVal);
	System.out.println("The String value is = " + stringVal);
}
}</pre>
</div>

Integer.toString()

The method Integer.toString() transforms an int to a String. The Integer class has a static function called toString(). The toString() method’s signature is as follows:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public static String toString(int i)</pre>
</div>

Integer.toString() is used to convert an int to a string in Java. This method is found in a lot of Java classes. It gives you a string as a result. Further, it can be used as a static function of the Integer class in the following way:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>Integer.toString(512)</pre>
</div>

Alternatively, you can utilize the Integer class object in its default form. Because you cannot use the primitive type int, remember to convert it to an Integer first by passing it to the constructor or by using the ‘=’ operator as shown in the example below:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>Integer intVar = 512;
intVar.toString();</pre>
</div>

Let’s look at some simple Java code that converts an int to a String using the Integer.toString() function.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>int intVar =10;
String stringVar=Integer.toString(intVar);//Now it will return "10"</pre>
</div>

Let’s look at a simple java example of converting String to int.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public class IntToStringCode{  
  public static void main(String args[]){  
    int intVal=125;  
    String stringVal=Integer.toString(intVal);  
    System.out.println(intVal+500);//625 because + is binary plus operator  
    System.out.println(stringVal+500);//125500 because + is string concatenation operator  
  }
}  </pre>
</div>

Use the getClass().getName() method to get the type of a variable.

Example:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public class main{
  public static void main(String[] args){
    
    // Converting 'int' to 'Integer'
    int intVal = 512;
    Integer stringVal = new Integer(intVal); // passing to constructor

    // Integer stringVal = intVal;   //  or use simple assignment

    System.out.println("Before conversion: " + stringVal.getClass().getName());

    System.out.println("After conversion: " + stringVal.toString().getClass().getName());
    System.out.println("After conversion (using Static method): " + Integer.toString(intVal).getClass().getName());
  }
}</pre>
</div>

Example: Convert using Integer.toString(int)

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	int intValOne = 5012;
	int intValTwo = -5012;
	String stringValOne = Integer.toString(intValOne);
	String strtringValTwo = Integer.toString(intValTwo);
	System.out.println("The first String is = " + stringValOne);
	System.out.println("The second String is = " + strtringValTwo);
}
}</pre>
</div>

Example: Conversion using Integer(int).toString()

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	int intVal = 5023;
	Integer obj = new Integer(intVal);
	String strVal = obj.toString();
	System.out.println("The String values is = " + strVal);
}
}</pre>
</div>

or

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	int intVal = 5023;
	String stringVal = new Integer(intVal).toString();
	System.out.println("The string value is = " + stringVal);
}
}</pre>
</div>

or

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	String stringVal = new Integer(5023).toString();
	System.out.println("The string value is = " + stringVal);
}
}</pre>
</div>

String.format()

To format supplied arguments into String, use the String.format() method. It’s been around since JDK 1.5.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>String.format("%%d", int)</pre>
</div>

Any string containing percent d is the first argument. The integer you want to convert is the second argument. This technique will substitute your integer for percent d. This method isn’t just for conversion: it may also be used for other things.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>String.format("%%d", 512);</pre>
</div>
<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public static String format(String format, Object... args)</pre>
</div>

String.format() is used to convert an int to a string in Java. Let’s look at some simple Java code that converts an int to a string using the String.format() function.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public class IntToStringCode{  
  public static void main(String args[]){  
    int intVal=125;  
    String stringVal=String.format("%%d",intVal);  
    System.out.println(stringVal);  
  }
}  
</pre>
</div>

Example: String.format()

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public class main{
  public static void main(String[] args){
    
    Integer intVal = new Integer(512);

    System.out.println("Results before the  conversion is: " + intVal.getClass().getName());
    
    System.out.println("Results after the conversion is: " + String.format("%%d", intVal).getClass().getName());
  }
}</pre>
</div>

Example: String.format() method for conversion

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public class CodeJavaStringFormat{  
   public static void main(String args[]){  
	int numVar = 189;  
	String stringVar = String.format("%%d",numVar);  
	System.out.println("Welcome to Code underscored "+stringVar);  
   }
}</pre>
</div>

StringBuffer or StringBuilder

StringBuffer or StringBuilder are two different types of string buffers. The append() method is used by these two classes to create a string. StringBuffer is a class for concatenating many values into a single String. StringBuilder is identical to StringBuffer; however, it is not thread-safe. We build an object from one of these two types and call this method with our integer as the parameter:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public class main{
  public static void main(String[] args){
    
    Integer intVal = new Integer(512);
    StringBuilder stringVal = new StringBuilder(); // or StringBuffer
    stringVal.append(intVal);

    System.out.println(stringVal);
  }
}</pre>
</div>

Example: Conversion using StringBuffer or StringBuilder

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	int intVal = 5023;
	StringBuffer stringBuffer = new StringBuffer();
	sb.append(intVal);
	String stringVal = stringBuffer.toString();
	System.out.println("The string value is = " + stringVal);
}
}</pre>
</div>

or

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	String stringVal = new StringBuffer().append(5023).toString();
	System.out.println("The string  value is = " + stringVal);
}
}</pre>
</div>

Example: StringBuilder

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	int intVal = 5023;
	StringBuilder sBuilder = new StringBuilder();
	sb.append(intVal);
	String stringVal = sBuilder.toString();
	System.out.println("The string value is = " + stringVal);
}
}</pre>
</div>

or

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	String stringVal = new StringBuilder().append(5023).toString();
	System.out.println("The string value is = " + stringVal);
}
}
</pre>
</div>

Using an empty string as a concatenation

Concatenation isn’t intended for conversion. However, you can concatenate your integer to an empty string because adding an integer to a string produces a string:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>String strVal = "" + 512;</pre>
</div>

Example 1: Using concatenation with empty String

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public class main{
  public static void main(String[] args){
    
    Integer intVal = new Integer(512);

    System.out.println("Results before the  conversion is: " + intVal.getClass().getName());
    
    System.out.println("Results after the conversion is : " + ("" + intVal).getClass().getName());
  }
}</pre>
</div>

Example 2: Using concatenation with empty String

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored {
	public static void main(String args[])
	{
		int intOne = 5023;
		int intTwo = -5023;
		String stringOne = "" + intOne;
		String stringTwo = "" + intTwo;
		System.out.println("The  first string value is = " + stringOne);
		System.out.println("The second string value is = " + stringTwo);
	}
}
</pre>
</div>

Conversion using the DecimalFormat

The Java.text.DecimalFormat class is responsible for converting an integer to a String.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>import java.text.DecimalFormat;
class Codeunderscored
{
public static void main(String args[])
{
	int intVal = 5023;
	DecimalFormat dFormat = new DecimalFormat("#");
	String stringVal = dFormat.format(intVal);
	System.out.println(stringVal);
}
}</pre>
</div>

or

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>import java.text.DecimalFormat;
class Codeunderscored
{
public static void main(String args[])
{
	int intVal = 5023;
	DecimalFormat dFormat = new DecimalFormat("#,###");
	String stringVal = dFormat.format(intVal);
	System.out.println(stringVal);
}
}</pre>
</div>

Using this approach, you can choose the number of decimal places and the comma separator for readability.

Conversion Using a Unique Radix

The base (radix) 10 is used in all of the instances above. The methods for converting to binary, octal, and hexadecimal systems are listed below. It’s also possible to use an arbitrary bespoke number system.

Example: Binary

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	int intVal = 255;
	String binaryStringValue = Integer.toBinaryString(intVal);
	System.out.println(binaryStringValue);
}
}</pre>
</div>

From the output above, 11111111 is the resultant binary representation of the given number 255.

Example: Octal

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	int intVal = 255;
	String octalStringValue = Integer.toOctalString(intVal);
	System.out.println(octalStringValue);
}
}</pre>
</div>

From the output of the code above, 377 is the resultant octal representation of the given number 255.

Example: Hexadecimal

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	int intVal = 255;
	String hexStringValue = Integer.toHexString(intVal);
	System.out.println(hexStringValue);
}
}</pre>
</div>

According to the output of the latter piece of code, the ff is the hexadecimal representation of the given number 255.

Radix/Base Customization

When you are converting an int to a String, you can use any custom base/radix. The base 7 number system is used in the following example.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class Codeunderscored
{
public static void main(String args[])
{
	int intVal = 255;
	
	String customStringValue = Integer.toString(intVal, 7);
	System.out.println(customStringValue);
}
}</pre>
</div>

When expressed in the base seven systems, 513 represents the number 255.

Example: Conversion of an int to String

The above-mentioned methods (String.valueOf() and Integer.toString()) are demonstrated in this program. We have two integer variables, and one of them is converted using the String.valueOf(int i) method, while the other is converted using the Integer.toString(int i) method.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>public class CodeIntToString {
    public static void main(String[] args) {
        
        /* Approach one: using valueOf() method
         * of String class.
         */
        int intVar = 621;
        String stringVar = String.valueOf(intVar);
        System.out.println("The string value is: "+stringVar);
        
        /* Approach 2: using toString() method
         * of Integer class
         */
        int intVarTwo = 378;
        String stringVarTwo = Integer.toString(intVarTwo);
        System.out.println("The second string is: "+stringVarTwo);
    }
}</pre>
</div>

Conclusion

In this article, we’ve learned how to convert an int to a string in Java. The String.valueOf() or Integer.toString() methods can be used to convert an int to a String.The String.valueOf(int i) method accepts an integer as a parameter and produces a string representing the int. We have utilized the String.format() method for the conversion.

If your variable is an int, you should use Integer.toString(int) or String.valueOf() instead. However, if your variable is already an instance of Integer (the wrapper class for the base type int), you should use its toString() function instead, as illustrated above. This method is inefficient because it creates an instance of the Integer class before performing the conversion. The String.valueOf(int i) method is the same as the Integer.toString(int i) method.It is a member of the Integer class and converts an integer value to a String. For example, if the supplied value is 512, the returned string value is “512.”

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *