JavaSE:一些工具類
阿新 • • 發佈:2020-06-24
001.包裝類
//自動裝箱:基本型別 -->包裝類
Integer d1 = 1; //通過反編譯發現本質是通過Integer.valueOf()方式
//自動拆箱:包裝類 --> 基本型別
Integer d2 = new Integer(2);
int d3 = d2; // //通過反編譯發現本質是通過Integer.intValue()方式
//裝箱:基本型別 -->包裝類
int data1 = 1;
Integer data2 = Integer.valueOf(data1);
//拆箱:包裝資料類-->基本資料型別
Integer data3 = new Integer(2);
int data4 = data3.intValue();
//包裝類 --> 字串String
Integer aa = new Integer(2);
String str = aa.toString();
//字串String -->包裝類
String str="123" ;
Integer bb = new Integer(str); //通過new
Integer cc = Integer.valueOf(str); //通過valueOf()
//字串String -->基本型別
String str="123";
int num = Integer.parseInt(str);
複製程式碼
0011.為什麼有了基本型別還需要包裝型別?
- 包裝類裡面具有一些有用的方法和屬性,如HashCode、ParseInt;
- 基本型別不能賦予null值;
- 如集合、泛型不能直接用基本型別;
class Student{
float score; //預設值為0.0f
}
因為score預設值是0.0f,代表學生成績為0.0f,無法表示學生缺考情況,如果score是引用資料型別的話,預設值是null,我們可以將score為null的時候代表學生缺考
複製程式碼
002.Math
System.out.println(Math.ceil(9.2)); //向上取整 10.0
System.out.println(Math.floor(9.8)); //向下取整 9.0
//四捨五入
System.out.println(Math.round(9.62)); //10
System.out.println(Math.round(-9.62)); //-10
//取平方根
System.out.println(Math.sqrt(-9)); //NaN
System.out.println(Math.sqrt(9)); //3.0
//隨機數
int num1 = (int) (Math.random()*10);
System.out.println(num1); //[0,9]的隨機數
int num2 = (int) (Math.random()*8)+2;
System.out.println(num2); //[2,9]的隨機數
//取絕對值
int num3 = Math.abs(-1);
System.out.println(num3); //1
複製程式碼
003.Random
Random random = new Random();
int num = random.nextInt(9)+2;
System.out.println(num); //[2,10]的隨機數
複製程式碼
004.Date
Date date = new Date();
System.out.println(date);//Sun Dec 01 19:37:33 GMT+08:00 2019
System.out.println(date.toLocaleString());//2019-12-1 19:38:02
//DateFormat 格式化日期
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateInstance();
String result = dateFormat.format(date);
System.out.println(result);
dateFormat = DateFormat.getDateTimeInstance();
result = dateFormat.format(date);
System.out.println(result); //2019-12-1 19:49:14
date = dateFormat.parse(result); //將字串日期轉為Date物件
System.out.println(date); //Sun Dec 01 19:50:28 GMT+08:00 2019
//SimpleDateFormat 格式化日期,也是DateFormat的子類
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String result = simpleDateFormat.format(date);
System.out.println(result); //2019年12月01日 19:55:47
//將字串轉為Date物件時,注意格式要一致
date = simpleDateFormat.parse("2019年12月01日 19:55:47");
System.out.println(date); //Sun Dec 01 19:55:47 GMT+08:00 2019
複製程式碼
005.Calendar
和 Date一樣都表示日期類,但對日期操作建議使用該類
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Date otherDate = calendar.getTime();
System.out.println(calendar.get(Calendar.YEAR));//獲取年份
System.out.println(calendar.get(Calendar.MONTH)+1); //獲取月份
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //獲取該月的日
System.out.println(calendar.get(Calendar.HOUR)); //獲取小時
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); //獲取小時(24小時制)
System.out.println(calendar.get(Calendar.MINUTE)); //獲取分
System.out.println(calendar.get(Calendar.SECOND));//獲取秒
複製程式碼
006.Object
0061.equals方法
在Object類中,equals方法將判斷兩個物件是否具有相同的引用;如果兩個物件具有相同的引用,則它們一定相等
0062.hashCode方法
雜湊碼(hash code)是由物件匯出的一個整型值
0061.hashCode()與equals()
- 兩個物件equals得到true,則hashcode是相等的;
- 兩個物件equals得到false,但hashcode有可能相等;
- 兩個物件的hashcode相等,則equals的值不一定是true;
- 兩個物件的hashcode不同,則equals的值一定是false;