# Java Integer方法的基础应用

**URL:** <https://www.isharkfly.com/t/java-integer/7021>\
**Category:** 程序语言\
**Created:** [June 25, 2013, 8:23pm UTC](https://www.isharkfly.com/t/java-integer/7021 "2013-06-25T20:23:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [June 25, 2013, 8:23pm UTC](https://www.isharkfly.com/t/java-integer/7021/1 "2013-06-25T20:23:11Z")

</div>

考察下面的代码，下面的代码将会输出不同的值。[code]public class JavaLangIntegerType {  
void test\_decode() {  
int val = 0;  
val = Integer.decode(“1”); System.out.println("val = " + val); //返回1  
val = Integer.decode(“2”); System.out.println("val = " + val); //返回2  
val = Integer.decode(“0xa”); System.out.println(“val = " + val); //返回10  
val = Integer.decode(“0xb”); System.out.println(“val = " + val); //返回11  
val = Integer.decode(”-1”); System.out.println("val = " + val); //返回-1  
//val = Integer.decode(“a”); System.out.println("val = " + val); //抛出 NumberFormatException  
}

void test\_parseInt() {  
Integer val = null;  
val = Integer.parseInt(“0”, 10); System.out.println(“val = " + val); //返回 0  
val = Integer.parseInt(“473”, 10); System.out.println(“val = " + val); //返回 473  
val = Integer.parseInt(”-0”, 10); System.out.println(“val = " + val); //返回 0  
val = Integer.parseInt(“FC”, 16); System.out.println(“val = " + val); //返回252  
val = Integer.parseInt(”-FF”, 16); System.out.println("val = " + val); //返回 -255  
val = Integer.parseInt(“1100110”, 2); System.out.println(“val = " + val); //返回 102  
val = Integer.parseInt(“2147483647”, 10); System.out.println(“val = " + val); //返回 2147483647  
val = Integer.parseInt(”-2147483648”, 10); System.out.println("val = " + val); //返回 -2147483648  
val = Integer.parseInt(“Kona”, 27); System.out.println("val = " + val); //返回 411787  
[//Integer.parseInt](https://Integer.parseInt)(“2147483648”, 10); //抛出 NumberFormatException  
[//Integer.parseInt](https://Integer.parseInt)(“99”, 8); //抛出 NumberFormatException  
[//Integer.parseInt](https://Integer.parseInt)(“Kona”, 10); //抛出 NumberFormatException  
}

void test\_toHexString() {  
String str = null;  
str = Integer.toHexString(0); System.out.println("str = " + str); //返回0  
str = Integer.toHexString(10); System.out.println("str = " + str); //返回a  
str = Integer.toHexString(20); System.out.println("str = " + str); //返回14  
str = Integer.toHexString(30); System.out.println("str = " + str); //返回1e  
str = Integer.toHexString(-1); System.out.println("str = " + str); //返回ffffffff  
str = Integer.toHexString(-2); System.out.println("str = " + str); //返回fffffffe  
}

void test\_toString() {  
String str = null;  
str = Integer.toString(1, 10); System.out.println("str = " + str); //返回1  
str = Integer.toString(10, 10); System.out.println("str = " + str); //返回10  
str = Integer.toString(10, 16); System.out.println("str = " + str); //返回a  
str = Integer.toString(20, 10); System.out.println("str = " + str); //返回20  
str = Integer.toString(20, 16); System.out.println("str = " + str); //返回14  
str = Integer.toString(30, 10); System.out.println("str = " + str); //返回30  
str = Integer.toString(30, 16); System.out.println("str = " + str); //返回1e  
str = Integer.toString(-1, 10); System.out.println("str = " + str); //返回-1  
str = Integer.toString(-2, 16); System.out.println("str = " + str); //返回-2  
}

void test\_valueOf() {  
Integer val = null;  
val = Integer.valueOf(0); System.out.println("val = " + val); //返回0  
val = Integer.valueOf(1); System.out.println("val = " + val); //返回 1  
val = Integer.valueOf(10); System.out.println("val = " + val); //返回 10  
val = Integer.valueOf(20); System.out.println("val = " + val); //返回 20  
val = Integer.valueOf(30); System.out.println("val = " + val); //返回 30  
val = Integer.valueOf(-1); System.out.println("val = " + val); //返回 -1

val = Integer.valueOf(“0”); System.out.println("val = " + val); //返回 0  
val = Integer.valueOf(“1”); System.out.println("val = " + val); //返回1  
val = Integer.valueOf(“10”); System.out.println("val = " + val); //返回10  
val = Integer.valueOf(“20”); System.out.println(“val = " + val); //返回20  
val = Integer.valueOf(“30”); System.out.println(“val = " + val); //返回30  
val = Integer.valueOf(”-1”); System.out.println("val = " + val); //返回-1

val = Integer.valueOf(“0”, 16); System.out.println("val = " + val); //返回0  
val = Integer.valueOf(“1”, 16); System.out.println("val = " + val); //返回1  
val = Integer.valueOf(“10”, 16); System.out.println("val = " + val); //返回10  
val = Integer.valueOf(“20”, 16); System.out.println(“val = " + val); //返回20  
val = Integer.valueOf(“30”, 16); System.out.println(“val = " + val); //返回30  
val = Integer.valueOf(”-1”, 16); System.out.println("val = " + val); //返回-1  
}  
}[/code]
