public class StringToNumber {
public static void main(String[] args) {
String str = "123";
Integer num1 = new Integer(str);
int num2 = Integer.parseInt(str);
Integer num3 = Integer.valueOf(str);
System.out.println(num1+"\t"+num2+"\t"+num3);
}
}
if it’s a single character or a string you have to cut it and convert it to char and then convert it to char otherwise it will be null pointer exception
like above
String a = "ABC";
//将String对象中的每一个下标位的对象保存在数组中
char[] b = a.toCharArray();
//转换成响应的ASCLL
for (char c : b) {
System.out.println(Integer.valueOf(c));
}
principle analysis:
Interger class constructor
directly into the source code, look at this constructor:
-
public Integer(String s) throws NumberFormatException {
-
this.value = parseInt(s, 10);
-
}
What is
this this.value?Continue tracing source:
private final int value;
from this, we can find a characteristic of the packaging class:
inside the wrapper, the corresponding primitive data type of the wrapper class is treated as a private attribute, and the value of the primitive data type is wrapped externally into an object of the corresponding wrapper class.
in the constructor of an Integer, we find that the parseInt (String s) method is called, and we continue our tracing, which leads us to the second method demonstrated at the beginning of this article:
parseInt (String s) method
-
public static int parseInt(String s) throws NumberFormatException {
-
return parseInt(s,10); code> p> li>
-
} code> p> li>
-
// a: the first method of encapsulation, code> p> li>
-
// a:
li b>
public static int parseInt(String s, int radix)throws NumberFormatException
valueOf (String s) method
-
public static Integer valueOf(String s) throws NumberFormatException {
-
return Integer.valueOf(parseInt(s, 10));
-
}
The last method,
, actually calls the parseInt() method (the second)
Read More:
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- JAVA 8: How to Convert List to Map
- JAVA: How to Convert PDF from Color to Grayscale
- JAVA: How to Delete the Last Bit of String
- The number of control threads and concurrency number of concurrent executor of Java starting gun
- How to get the current time in java time string
- [Solved] Java Run Error: For input string: “XXX”
- [Solved] Read the resources resource and convert it to file error: java.io.filenotfoundexception
- [Solved] stream Convert list to map Error: java.lang.IllegalStateException: Duplicate key
- Java will convert Excel to list set or JSON, export excel file to local, excel import and export, easyexcel tool class
- [Solved] SpringBoot Date Convert Error: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime`
- [Solved] Swagger Startup Error: java.lang.NumberFormatException: For input string: ““
- Problems and causes of Java’s main function format (public static void main (string args()))
- How to Converte Java objects to jsonnode in Jackson (Four Methods)
- [Solved] Java POI export error: Invalid row number (65536) outside allowable range
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- Idea error: (44,22) Java: constant string too long
- JAVA: How to Read JSON Format Data (Web Game Development)
- How to Solve Error:java.io.InvalidClassException
- How to Solve java server error (java application Run Normally)