1.0 int是java提供的8種原始資料類型之一。Java為每個原始類型提供了封裝類,Integer是java為int提供的封裝類。
2.0 int的預設值為0, 而Integer的預設值為null
3.0 另外,Integer提供了多個與整數相關的操作方法,例如,将一個字元串轉換成整數,
Integer中還定義了表示整數的最大值和最小值的常量。
即Integer可以區分出未指派和值為0的差別,int則無法表達出未指派的情況,例如,要想表達出沒有參加考試和考試成績
為0的差別則隻能使用Integer。
在JSP開發中,Integer的預設為null,是以用el表達式在文本框中顯示時,值為空白字元串,
而int預設的預設值為0,是以用el表達式在文本框中顯示時結果為0,是以,int不适合作為web層的表單資料的類型。
在Hibernate中,如果将OID(對象辨別符又稱為物聯網域名)定義為Integer類型,那麼Hibernate就可以根據其值
是否為null而判斷一個對象是否是臨時的,如果将OID定義為了int類型,還需要在hbm映射檔案中設定其unsaved-value
屬性為0。
/**
* 問題:要想表達出沒有參加考試和考試成績為0的差別?我們應該用Integer表示還是用int表示?
*/
public class A2015年6月4日_Integer和int {
private static int score;
private static Integer score2;
// private static boolean ss;
public static void main(String[] args) {
System.out.println("int類型的預設值score2:" + score);// 0
System.out.println("Integer類型的預設值score:" + score2);// null
/*
* if(score==null){//報錯因為score是int類型的不能和null比較
*
* }
*/
// if(ss==true)
// score2 = 0;
if (score2 == null) {
System.out.println("沒有參加考試!!!");
} else if (score2 == ) {
System.out.println("考試成績為0分!!!");
} else {
System.out.println("考試成績為" + score2);
}
integer();
}
public static void integer() {
String string = "12345";
Integer i = Integer.parseInt(string);// 把字元串解析為Integer類型
Integer max = Integer.MAX_VALUE;
Integer min = Integer.MIN_VALUE;
System.out.println("Integer.parseInt(string)=" + i);
System.out.println("integer的最大值:"+max+",最小值:"+min);
}
}