Convert String to Int in Java



For a given String , our task is to write a Java program that converts it into an Integer. In Java, String is a class in java.lang package that stores a series of characters enclosed within double quotes. Integer is a primitive datatype that stores numerical values.

Converting a String to int in Java

We can use the following methods to convert a string into an integer in Java:

  • Integer.parseInt()

  • Integer.valueOf()

Let's discuss these methods one by one with the help of example programs.

Using Integer.parseInt()

The Integer.parseInt() is a static method that belongs to the Integer class of java.lang package. It takes a string as a parameter and parses it into a primitive integer value. The following is the syntax of the Integer.parseInt() method -

Integer.parseInt(String val);

Example

The following Java program demonstrates how to convert a String into int using the parseInt() method.

Open Compiler
public class Example1 { public static void main( String args[] ) { // initializing a string String inputString = "99999"; // to check the datatype of string System.out.print("Type of inputString: "); System.out.println(inputString.getClass().getSimpleName()); // converting the string into an integer int newVal = Integer.parseInt(inputString); // printing the result System.out.println("The given String after converting into Integer: " + newVal); // to check the datatype of integer System.out.print("Type of given String after converting into Integer: "); System.out.println(((Object)newVal).getClass().getSimpleName()); } }

The above code will show the following result on running:

Type of inputString: String
The given String after converting into Integer: 99999
Type of given String after converting into Integer: Integer

Using Integer.valueOf()

The Integer.valueOf() is also a static method of Integer class. This method can accept a string, a char or an int as a parameter but returns an Integer object that holds the value of the parsed integer. Given below is the syntax -

Integer.valueOf(String val);

Example

This Java program will show how we can convert a String into int with the help of Integer.valueOf() method.

Open Compiler
public class Example2 { public static void main( String args[] ) { // initializing a string String inputString = "30072023"; // to check the datatype of string System.out.print("Type of inputString: "); System.out.println(inputString.getClass().getSimpleName()); // converting the string into an integer int newVal = Integer.valueOf(inputString); // printing the result System.out.println("The given String after converting into Integer: " + newVal); // to check the datatype of integer System.out.print("Type of given String after converting into Integer: "); System.out.println(((Object)newVal).getClass().getSimpleName()); } }

You will get the following output when you run the above code:

Type of inputString: String
The given String after converting into Integer: 30072023
Type of given String after converting into Integer: Integer
Updated on: 2025-04-17T10:42:52+05:30

446 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements