Convert string to int
In this chapter you will learn:
Decode a string and return an integer value
static Integer decode(String nm)
decodes a String into an Integer.
decode(String nm) accepts decimal, hexadecimal, and octal numbers given by the following grammar:
public class Main {
public static void main(String[] args) {
//from jav a 2 s . com
System.out.println(Integer.decode("010"));
}
}
The output:
Convert string to integer
Converting string to int type value can be done through the following methods.
static int parseInt(String s)
parses the string argument as a signed decimal integer.static int parseInt(String s, int radix)
parses the string argument in the radix specified by the second argument.static Integer valueOf(int i)
returns a Integer instance representing the specified int value.static Integer valueOf(String s)
returns an Integer object holding the value of the specified String.static Integer valueOf(String s, int radix)
returns an Integer from String based on the radix.
public class Main {
public static void main(String[] args) {
// ja v a 2 s. c o m
System.out.println(Integer.parseInt("010"));
}
}
The output:
You can also indicate the radix.
public class Main {
public static void main(String[] args) {
// j a va 2 s. com
System.out.println(Integer.parseInt("010",8));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Primitive Data Types