0 0
WP Ad Inserter plugin
Read Time:10 Minute, 56 Second

Table of Contents

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.

SUGGESTED READ

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:

public static String valueOf(int i)

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:

String.valueOf(Integer(512));

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.

SUGGESTED READ

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.

int intVal=10; String stringVal=String.valueOf(intVal);//Now it will return "10"

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

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 }
} 

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

Example : String.valueOf()

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()); }
}

Example: Conversion using String.valueOf(int)

class Codeunderscored
{
public static void main(String args[])
{ int intVal= 5012; String stringVal = String.valueOf(intVal); System.out.println("The String value is = " + stringVal);
}
}

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:

public static String toString(int i)

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:

SUGGESTED READ

Integer.toString(512)

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:

Integer intVar = 512;
intVar.toString();

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

int intVar =10;
String stringVar=Integer.toString(intVar);//Now it will return "10"

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

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 }
} 

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

Example:

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()); }
}

Example: Convert using Integer.toString(int)

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);
}
}

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

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);
}
}

or

SUGGESTED READ

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);
}
}

or

class Codeunderscored
{
public static void main(String args[])
{ String stringVal = new Integer(5023).toString(); System.out.println("The string value is = " + stringVal);
}
}

String.format()

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

String.format("%d", int)

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.

String.format("%d", 512);
public static String format(String format, Object... args)

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.

public class IntToStringCode{ public static void main(String args[]){ int intVal=125; String stringVal=String.format("%d",intVal); System.out.println(stringVal); }
} 

Example: String.format()

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()); }
}

Example: String.format() method for conversion

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); }
}

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:

SUGGESTED READ

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); }
}

Example: Conversion using StringBuffer or StringBuilder

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);
}
}

or

class Codeunderscored
{
public static void main(String args[])
{ String stringVal = new StringBuffer().append(5023).toString(); System.out.println("The string value is = " + stringVal);
}
}

Example: StringBuilder

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);
}
}

or

class Codeunderscored
{
public static void main(String args[])
{ String stringVal = new StringBuilder().append(5023).toString(); System.out.println("The string value is = " + stringVal);
}
}

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:

String strVal = "" + 512;

Example 1: Using concatenation with empty String

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()); }
}

Example 2: Using concatenation with empty String

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); }
}

Conversion using the DecimalFormat

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

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);
}
}

or

SUGGESTED READ

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);
}
}

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

class Codeunderscored
{
public static void main(String args[])
{ int intVal = 255; String binaryStringValue = Integer.toBinaryString(intVal); System.out.println(binaryStringValue);
}
}

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

Example: Octal

class Codeunderscored
{
public static void main(String args[])
{ int intVal = 255; String octalStringValue = Integer.toOctalString(intVal); System.out.println(octalStringValue);
}
}

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

Example: Hexadecimal

class Codeunderscored
{
public static void main(String args[])
{ int intVal = 255; String hexStringValue = Integer.toHexString(intVal); System.out.println(hexStringValue);
}
}

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

SUGGESTED READ

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.

class Codeunderscored
{
public static void main(String args[])
{ int intVal = 255; String customStringValue = Integer.toString(intVal, 7); System.out.println(customStringValue);
}
}

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.

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); }
}

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.”

SUGGESTED READ

Source: https://www.codeunderscored.com/convert-an-integer-to-a-string-in-java/

Previous post How to convert character array to string in Java
Write HTML, the HTML Way (Not the XHTML Way) Next post Write HTML, the HTML Way (Not the XHTML Way)

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.