天天看點

java中praseInt()和valueOf()的差別

相同點:這兩個方法是Integer對象的方法

差別:主要在于傳回類型不一樣

praseInt() 傳回值為int對象 

e.g.  int i = Integer.praseInt(String s);

valueOf()傳回值為Integer,可以使用Integer對象裡面的所有方法,包括轉為Object對象,而int類型不能轉為Object對象,

如果使用valueOf方法并賦給了int類型,jre就會幫我們把它自動轉為int對象.

e.g. int i = Integer.valueOf(String s);

看似沒什麼差別,但是valueOf中已經把Integer轉為int類型了