How to Convert String to Number?

There is multiple way to Convert String to Number in java.

Integer valueOf() Method :-

The java.lang.Integer.valueOf(int a) is an inbuilt method which is used to return an Integer object.

Now intValue() method convert Integer object to primitive type int.

Syntax:- public static Integer valueOf(int a)

Using Built in Functions:-

Here we are using Integer.parseInt() method to convert String to Number.

					public class StringToNumberDemo {
	public static void main(String[] args) {

		String str = "123";
		// from Integer valueOf() Method
		int d = Integer.valueOf(str).intValue();
		System.out.println("from valueOf method :- " + d);

		// Integer.parseInt() method
		int i = Integer.parseInt(str);
		System.out.println("from built in method value :- " + i);


Output :-
from valueOf method value :- 123
from built in method value :- 123