Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9ae0d7f

Browse files
committed
converting-string-to-int-in-java
1 parent d29bed2 commit 9ae0d7f

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

‎README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ stackoverflow-Java-top-qa
1717
* [如何判断数组Array是否包含指定的值?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/in-java-whats-the-difference-between-public-default-protected-and-private.md)
1818
* [重写(Override)equlas和hashCode方法时应考虑的问题](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java.md)
1919
* [从一个多层嵌套循环中直接跳出](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/breaking-out-of-nested-loops-in-java.md)
20-
20+
*[如何将String转换为Int](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/converting-string-to-int-in-java.md)
2121

2222
> 编程技巧
2323
@@ -44,7 +44,6 @@ stackoverflow-Java-top-qa
4444
- [Generating random integers in a range with Java](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java)
4545
- [Why is printing "B" dramatically slower than printing "#"?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing)
4646
- [What is a serialVersionUID and why should I use it?](http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it)
47-
- [Converting String to int in Java?](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java)
4847
- [Is there a unique Android device ID?](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id)
4948
- [How to test a class that has private methods, fields or inner classes](http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes)
5049
- [Why does this code using random strings print "hello world"?](http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
##如何将String转换为Int
2+
3+
有两种方式
4+
5+
```java
6+
Integer x = Integer.valueOf(str);
7+
// or
8+
int y = Integer.parseInt(str);
9+
```
10+
这两种方式有一点点不同:
11+
- `valueOf`返回的是`java.lang.Integer`的实例
12+
- `parseInt`返回的是基本数据类型 int
13+
14+
`Short.valueOf/parseShort`,`Long.valueOf/parseLong`等也是有类似差别。
15+
16+
另外还需注意的是,在做int类型转换时,可能会抛出NumberFormatException,因此要做好异常捕获
17+
```java
18+
int foo;
19+
String StringThatCouldBeANumberOrNot = "26263Hello"; //will throw exception
20+
String StringThatCouldBeANumberOrNot2 = "26263"; //will not throw exception
21+
try {
22+
foo = Integer.parseInt(StringThatCouldBeANumberOrNot);
23+
} catch (NumberFormatException e) {
24+
//Will Throw exception!
25+
//do something! anything to handle the exception.
26+
}
27+
28+
try {
29+
foo = Integer.parseInt(StringThatCouldBeANumberOrNot2);
30+
} catch (NumberFormatException e) {
31+
//No problem this time but still it is good practice to care about exceptions.
32+
//Never trust user input :)
33+
//do something! anything to handle the exception.
34+
}
35+
```
36+
37+
stackoverflow链接:http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /