Why and how to use Integer.parseInt() in java

Integer.parseInt() method is used to convert an string value into integer. JVM reads each element as String and hence we have to convert an entered String value into respective data type

Why and how to use Integer.parseInt() in java

Integer.parseInt() method is used to convert an string value into integer. JVM reads each element as String and hence we have to convert an entered String value into respective data type

Why and how to use Integr.parseInt in java

Why and how to use Integer.parseInt() in java.

Integer.parseInt() method is used to convert an string value into integer. JVM reads each element as String and hence we have to convert an entered String value into respective data type. Integer.parseInt() is such a method in Java to convert String input into the Integer. In  java , we know that everything treated as  String format. Now let us suppose we  enter a value in int primitive type, then we have to use int a = Integer.parseInt("String") , because the value we have entered is in integer but it will be treated as as a String so, we have to change the string into integer. Here Integer is a name of a class in java.lang package and parseInt() is a method of Integer class which converts String to integer. If we want to enter a integer in a method or class using keyboard, then we have to use a method parseInt().

Example:

How to use intger.parseInt in java.
public class StringToIntExample {
	public static void main(String[] args) {
		String string = new String("555"); //declare String object
		int i = Integer.parseInt(string); //the Integer parseint method of integer class to convert String into int primitive data type.
		System.out.println("Result is: " + i);
		
         	}	
       }
Here in above example"555"is taking as string and through integet.parseInt() method we are converting it into integer.

Output:
 After compiling and executing the above program.

Download Source Code