1. 程式人生 > >java中Integer.parseInt(String)與Integer.valueOf(String)區別

java中Integer.parseInt(String)與Integer.valueOf(String)區別

介面描述:

valueOf() 
返回一個Integer物件持有指定的原始值

parseInt() 
此方法用於獲取某個字串的原始資料型別

說白了,Integer.valueOf(String)是將String轉化為Integer物件

Integer.parseInt(String)是將String轉化為int

直接上程式碼:

package helloproject;

public class helloworld {

	public static void main(String[] args) {
		
		Integer a = 10;
		System.out.println(a.compareTo(11));
		System.out.println(a.compareTo(10));
		System.out.println(a.compareTo(9));
		
		System.out.println(a.valueOf(1112));
		System.out.println(a.valueOf(10));

		System.out.println(a.valueOf("112"));		
		System.out.println(a.parseInt("10"));		
		System.out.println(a.parseInt("11"));
		
		boolean r1 = a.valueOf("112") instanceof Integer;
		int kk = a.parseInt("112");
		boolean r2 = a instanceof Integer;
		System.out.println(r1);
		System.out.println(r2);		
	}

}

執行結果:
-1
0
1
1112
10
112
10
11
true
true