In this article, we will show you how to convert Java String to wrapper Integer class or primitive int code examples. There are different ways we can convert Java String to wrapper Integer class or primitive int.
A common requirement while programming in Java is to convert String to an int. UI inputs in Web-based HTML, JSP, or Thymeleaf templates are transferred to backend Java applications as strings. It is the application developer’s responsibility to perform any String to int conversions to fulfill business logic, such as calculating discounts, storing age, and so on.
This is a common scenario to convert String to wrapper Integer class or primitive int in Java programming language.
A common requirement while programming in Java is to convert String to an int. UI inputs in Web-based HTML, JSP, or Thymeleaf templates are transferred to backend Java applications as strings. It is the application developer’s responsibility to perform any String to int conversions to fulfill business logic, such as calculating discounts, storing age, and so on.
This is a common scenario to convert String to wrapper Integer class or primitive int in Java programming language.
In this post, we will demonstrate converting String to wrapper Integer class or primitive int in 4 ways.
- Convert using Integer.parseInt()
- Convert using Integer.valueOf()
- Convert using new Integer(String).intValue()
- Convert using DecimalFormat
1. Convert using Integer.parseInt()
Integer.parseInt() - Parses the string argument as a signed decimal integer.
// Convert using Integer.parseInt()
String str = "1234";
int number = Integer.parseInt(str);
System.out.println("Convert using Integer.parseInt() : " + number);
Output:
Convert using Integer.parseInt() : 1234
2. Convert using Integer.valueOf()
The Integer.valueOf() static method will return an Integer object holding the value of the specified String.
// Convert using Integer.valueOf()
String str = "1234";
int number1 = Integer.valueOf(str);
System.out.println("Convert using Integer.valueOf() : " + number1);
Output:
Convert using Integer.valueOf() : 1234
3. Convert using new Integer(String).intValue()
Another alternative method is to create an instance of Integer class and then invoke it's intValue() method.
// Convert using new Integer(String).intValue()
String str = "1234";
int number2 = new Integer(str).intValue();
System.out.println("Convert using new Integer(String).intValue() : " + number2);
Output:
Convert using new Integer(String).intValue() : 1234
4. Convert using DecimalFormat
The class java.text.DecimalFormat is a class that can be used to convert a number to its String representation. It can also be used the other way around - it can parse a String into its numerical representation.
// Convert using DecimalFormat
String str = "1234";
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
int number3 = decimalFormat.parse(str).intValue();
System.out.println("Convert using DecimalFormat : " + number3);
} catch (ParseException e) {
System.out.println(str + " is not a valid number.");
}
Output:
Convert using DecimalFormat : 1234
Complete Program for Reference
import java.text.DecimalFormat;
import java.text.ParseException;
/**
* ConvertStringToIntegerExamples
* @author javaguides.net
*
*/
public class ConvertStringToIntegerExamples {
public static void main(String[] args) {
// Convert using Integer.parseInt()
String str = "1234";
int number = Integer.parseInt(str);
System.out.println("Convert using Integer.parseInt() : " + number);
// Convert using Integer.valueOf()
int number1 = Integer.valueOf(str);
System.out.println("Convert using Integer.valueOf() : " + number1);
// Convert using new Integer(String).intValue()
int number2 = new Integer(str).intValue();
System.out.println("Convert using new Integer(String).intValue() : " + number2);
// Convert using DecimalFormat
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
int number3 = decimalFormat.parse(str).intValue();
System.out.println("Convert using DecimalFormat : " + number3);
} catch (ParseException e) {
System.out.println(str + " is not a valid number.");
}
}
}
Output:
Convert using Integer.parseInt() : 1234
Convert using Integer.valueOf() : 1234
Convert using new Integer(String).intValue() : 1234
Convert using DecimalFormat : 1234
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course