Java 常用类库
文章目錄
- 1. String、StringBuffer
- 2. 基本數(shù)據(jù)類
- 3. System 類
- 4. Runtime 類
- 5. Date、Calendar、DateFormat 類
- 6. Math、Random 類
- 7. hashCode() 方法
- 8. 對象克隆
1. String、StringBuffer
public class testString {public static void main(String [] args){String s = "Michael";s += ", ok";System.out.println(s);StringBuffer x = new StringBuffer("Ming");x.append("ming").append(", good!");System.out.println(x.toString());// 頻繁修改字符串,使用 StringBuffer 性能更好} }輸出:
Michael, ok Mingming, good!2. 基本數(shù)據(jù)類
String s1 = "10"; // Integer.parseInt 字符串轉(zhuǎn)整數(shù) int n = Integer.parseInt(s1,16);// x 進(jìn)制, 不寫默認(rèn)10進(jìn)制 System.out.println(++n); // 173. System 類
- 該類中所有成員都是靜態(tài)的
4. Runtime 類
Runtime run = Runtime.getRuntime(); try{run.exec("notepad.exe");// 打開記事本,exec返回子進(jìn)程Process類對象 } catch (Exception ep){ep.printStackTrace(); }5. Date、Calendar、DateFormat 類
// 時(shí)間類 Calendar c1 = Calendar.getInstance(); System.out.println(c1.get(c1.YEAR)+"年"+(c1.get(c1.MONTH)+1)+"月"+c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR_OF_DAY)+":"+c1.get(c1.MINUTE)+":"+c1.get(c1.SECOND));c1.add(c1.DAY_OF_MONTH, 3); // 加3天 System.out.println(c1.get(c1.YEAR)+"年"+(c1.get(c1.MONTH)+1)+"月"+c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR_OF_DAY)+":"+c1.get(c1.MINUTE)+":"+c1.get(c1.SECOND));輸出:
2021年2月26日23:5:26 2021年3月1日23:5:26 SimpleDateFormat sp1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); SimpleDateFormat sp2 = new SimpleDateFormat("yyyy年MM月dd日 hh點(diǎn)mm分ss秒"); try{Date d = sp1.parse("2021-2-26 23:02:03");System.out.println(sp2.format(d));// 2021年02月26日 11點(diǎn)02分03秒 } catch (ParseException e2){e2.printStackTrace(); }6. Math、Random 類
Random r = new Random();//以時(shí)間為種子的 for(int i = 0; i < 5; ++i)System.out.print(r.nextInt(100)+"\t");7. hashCode() 方法
- 存取散列表的時(shí)候,需要編寫該方法
8. 對象克隆
- 實(shí)現(xiàn) Cloneable 接口
- 改寫 Object 類 的 clone() 方法
輸出:
兩個(gè)對象的地址比較:false name: Michael age: 18 name: Ming age: 19總結(jié)
- 上一篇: LeetCode 2037. 使每位学生
- 下一篇: Java enum枚举