Skip to content

Commit dd50662

Browse files
类型转换会遇到的坑
1 parent 4619df9 commit dd50662

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.string;
2+
3+
import org.apache.commons.lang.ObjectUtils;
4+
import org.apache.commons.lang.StringUtils;
5+
import org.apache.commons.lang.math.NumberUtils;
6+
7+
/**
8+
* @Author: EnjoyCoding
9+
* @Date: 2020\3\30 0030 23:58
10+
* @Description:
11+
*/
12+
public class StringToIntegerDemo {
13+
14+
15+
public static void main(String[] args) {
16+
// objectToString();
17+
// nullToEmpty();
18+
// strToInteger();
19+
integerValueOfDemo();
20+
}
21+
22+
public static void nullToEmpty() {
23+
Object object=null;
24+
String str = ObjectUtils.toString(object);
25+
// String str = ObjectUtils.toString(object,null);
26+
System.out.println("值为:"+str);
27+
}
28+
29+
public static void strToInteger() {
30+
String str="abc";
31+
//str不为数字时,设置默认值为 0
32+
int num = NumberUtils.toInt(str);
33+
//str不为数字时,设置默认值为其他值
34+
int defaultNum=NumberUtils.toInt(str,123);
35+
}
36+
37+
public static void integerValueOfDemo() {
38+
String str="123";
39+
// String str="";
40+
//str为非数字字符串时,报错: NumberFormatException:""
41+
Integer value = Integer.valueOf(str);
42+
System.out.println(value);
43+
}
44+
}

0 commit comments

Comments
 (0)