Convert String to int in Java
Some times we need to convert String to int in Java for android purposes. This is fairly easy using Integer class.
This can be achieved by two methods:
1. Using Integer.parseInt()
Using parseInt() method of Integer class we can convert String to int
1 2 3 |
String number = "10"; int result = Integer.parseInt(number); System.out.println(result); |
Note : In this case the number returned by the method is primitive int.
2. Using Integer.valueOf()
Using valueOf() method we can convert String to Integer object.
1 2 3 |
String number = "10"; Integer result = Integer.valueOf(number); System.out.println(result); |
Note : In this case we will instance of Integer class.