一、字符串相關(guān)類
1.String類
作用:表示字符串,所有字符串文字(例如“abc”)都為String的實(shí)例
使用:
- 字符串常量池中是否存在“abc”,如果存在“abc”就不創(chuàng)建直接使用
- new String(“abc”);在堆中創(chuàng)建對象
特點(diǎn):被final修飾,不可變字符串,一旦改變則為新建對象。
public class TestString1 {
public static void main(String[] args) {
String s1 = new String("abcdef");
String s2 = s1.substring(2, 4);// 打?。篴b199863
System.out.println(Integer.toHexString(s1.hashCode())); // 打?。篶61, 顯然 s1 和 s2 不是同一個(gè)對象
System.out.println(Integer.toHexString(s2.hashCode()));
}
}
2. StringBuilder 類
可變長度字符序列,線程不安全,效率高
常用方法列表:
- 重載的 public StringBuilder append(…)
方法可以為該 StringBuilder 對象添加字符序列,仍然返回自身對象。 - 方法 public StringBuilder delete(int start,int end)
可以刪除從 start 開始到 end-1 為止的一段字符序列,仍然返回自身對象。 - 方法 public StringBuilder deleteCharAt(int index)
移除此序列指定位置上的 char,仍然返回自身對象。 - 重載的 public StringBuilder insert(…)方法
可以為該 StringBuilder 對象在指定位置插入字符序列,仍然返回自身對象。 - 方法 public StringBuilder reverse()
用于將字符序列逆序,仍然返回自身對象。 - 方法 public String toString()
返回此序列中數(shù)據(jù)的字符串表示形式。
3. StringBuffer 類
可變長字符序列,線程安全,效率低
常用方法列表:
- 重載的 public StringBuffer append(…)
方法可以為該 StringBuffer 對象添加字符序列,仍然返回自身對象。 - 方法 public StringBuffer delete(int start,int end)
可以刪除從 start 開始到 end-1 為止的一段字符序列,仍然返回自身對象。 - 方法 public StringBuffer deleteCharAt(int index)
移除此序列指定位置上的 char,仍然返回自身對象。 - 重載的 public StringBuffer insert(…)方法
可以為該 StringBuffer 對象在指定位置插入字符序列,仍然返回自身對象。 - 方法 public StringBuffer reverse()
用于將字符序列逆序,仍然返回自身對象。 - 方法 public String toString()
返回此序列中數(shù)據(jù)的字符串表示形式。
4.StringBuffer和StringBuilder的使用
public class TestStringBufferAndBuilder {
public static void main(String[] args) { /**StringBuilder*/StringBuilder sb = new StringBuilder();
for (int i = 0; i < 7; i++) {
sb.append((char) ('a' + i));//追加單個(gè)字符
}
System.out.println(sb.toString());//轉(zhuǎn)換成 String 輸出
sb.append(", I can sing my abc!");//追加字符串
System.out.println(sb.toString());
/**
* StringBuffer,下面的方法同樣適用 StringBuilder
*/
StringBuffer sb2 = new StringBuffer("北京尚學(xué)堂");
sb2.insert(0, "愛").insert(0, "我");//插入字符串
System.out.println(sb2);
sb2.delete(0, 2);//刪除子字符串
System.out.println(sb2);
sb2.deleteCharAt(0).deleteCharAt(0);//刪除某個(gè)字符
System.out.println(sb2.charAt(0));//獲取某個(gè)字符
System.out.println(sb2.reverse());//字符串逆序
}
}
執(zhí)行效率:StringBuilder > StringBuffer > String
二、包裝類
1. 基本數(shù)據(jù)類型 --> 包裝類
| 基本數(shù)據(jù)類型 | 包裝類 |
|---|---|
| byte | Byte |
| int | Integer |
| long | Long |
| short | Short |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
在這八個(gè)類名中,除了 Integer 和 Character 類以外,其它六個(gè)類的類名和基本數(shù)據(jù) 類型一致,只是類名的第一個(gè)字母大寫而已。 在這八個(gè)類中,除了 Character 和 Boolean 以外,其他的都是“數(shù)字型”,“數(shù)字 型”都是 java.lang.Number 的子類。

作用:
- 作為和基本數(shù)據(jù)類型對應(yīng)的類型存在,方便涉及到對象的操作,如 Object[ ]、集合等 的操作。
- 包含每種基本數(shù)據(jù)類型的相關(guān)屬性如最大值、最小值等,以及相關(guān)的操作方法(這些操 作方法的作用是在基本數(shù)據(jù)類型、包裝類對象、字符串之間提供相互之間的轉(zhuǎn)化?。?。
2. 自動拆裝箱
自動裝箱:
int i= 1;
Integer in = i;
自動拆箱:
int i2 = in;
自動拆箱為包裝類和基本數(shù)據(jù)類型的比較(int和Integer)數(shù)值相等時(shí):
- 兩個(gè)基本數(shù)據(jù)類型相比,肯定相等
- 兩個(gè)通過new構(gòu)建的Integer對象不相等,地址不同
- 當(dāng)基本數(shù)據(jù)類型int與對應(yīng)的包裝類型Integer比較都相等,包裝類會自動拆箱再比較
- 如果兩個(gè)Integer比較(Integer.valueOf()),內(nèi)部會新建比較是否再緩存區(qū)中,如故宮不在會新建對象,如果過在則直接使用
三、時(shí)間相關(guān)類
在計(jì)算機(jī)世界,我們把 1970 年 1 月 1 日 00:00:00 定為基準(zhǔn)時(shí)間,每個(gè)度量單 位是毫秒(1 秒的千分之一)
我們用 long 類型的變量來表示時(shí)間,從基準(zhǔn)時(shí)間往前幾億年,往后幾億年都能表示。 如果想獲得現(xiàn)在時(shí)刻的“時(shí)刻數(shù)值”,可以使用:long now = System.currentTimeMillis();

1. Date 時(shí)間類(java.util.Date)
在標(biāo)準(zhǔn) Java 類庫中包含一個(gè) Date 類。它的對象表示一個(gè)特定的瞬間,精確到毫秒。
Date類的常用方法:
- Date() 分配一個(gè) Date 對象,并初始化此對象為系統(tǒng)當(dāng)前的日期和時(shí)間,可以精 確到毫秒)。
- Date(long date) 分配 Date 對象并初始化此對象,以表示自從標(biāo)準(zhǔn)基準(zhǔn)時(shí)間(稱 為“歷元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以來的指定毫秒數(shù)。
- boolean after(Date when) 測試此日期是否在指定日期之后。
- booleanbefore(Date when) 測試此日期是否在指定日期之前。
- boolean equals(Object obj) 比較兩個(gè)日期的相等性。
- long getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 對 象表示的毫秒數(shù)。
- String toString() 把此 Date 對象轉(zhuǎn)換為以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中:dow 是一周中的某一天 (Sun、Mon、 Tue、 Wed、 Thu、 Fri、 Sat)。
Date d = new Date(); //當(dāng)前時(shí)刻的對象
System.out.println(d.getTime()); //返回時(shí)間對應(yīng)的毫秒數(shù)
Date d2 = new Date(1000L * 3600 * 24 * 365 * 150); //距離 1970 年 150 年
2. DateFormat 類和 SimpleDateFormat 類
DateFormat 的作用:
把時(shí)間對象轉(zhuǎn)化成指定格式的字符串。反之,把指定格式的字符串轉(zhuǎn)化成時(shí)間對象。 DateFormat 是一個(gè)抽象類,一般使用它的的子類 SimpleDateFormat 類來實(shí)現(xiàn)。
SimpleDateFormat sdm = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
System.out.println(sdm.format(new Date()));
例:
public class TestDateFormat2 {
public static void main(String[ ] args) {
SimpleDateFormat s1 = new SimpleDateFormat("D");
String daytime = s1.format(new Date()); System.out.println(daytime);
}
}
四、數(shù)學(xué)運(yùn)算類
1. Math類
java.lang.Math 提供了一系列靜態(tài)方法用于科學(xué)計(jì)算;其方法的參數(shù)和返回值類型一 般為 double 型。如果需要更加強(qiáng)大的數(shù)學(xué)運(yùn)算能力,計(jì)算高等數(shù)學(xué)中的相關(guān)內(nèi)容,可以使用 apache commons 下面的 Math 類庫。
常用方法:
abs 絕對值
acos,asin,atan,cos,sin,tan 三角函數(shù)
sqrt 平方根
pow(double a, double b) a 的 b 次冪
max(double a, double b) 取大值
min(double a, double b) 取小值
ceil(double a) 大于 a 的最小整數(shù)
floor(double a) 小于 a 的最大整數(shù)
random() 返回 0.0 到 1.0 的隨機(jī)數(shù)
? 1. [max,min) = (int)(Math.random()*(max-min)+min);
? 2. [max,min] = (int)(Math.random()*(max-min+1)+min);
long round(double a) double 型的數(shù)據(jù) a 轉(zhuǎn)換為 long 型(四舍五入)
toDegrees(double angrad) 弧度->角度
toRadians(double angdeg) 角度->弧度
2. Random 類
Math 類中雖然為我們提供了產(chǎn)生隨機(jī)數(shù)的方法 Math.random(),但是通常我們需要的隨機(jī)數(shù)范圍并不是[0, 1)之間的 double 類型的數(shù)據(jù),這就需要對其進(jìn)行一些復(fù)雜的運(yùn)算。 如果使用 Math.random()計(jì)算過于復(fù)雜的話,我們可以使用例外一種方式得到隨機(jī)數(shù),即 Random 類,這個(gè)類是專門用來生成隨機(jī)數(shù)的,并且 Math.random()底層調(diào)用的就是 Random 的 nextDouble()方法。
public class TestRandom {
public static void main(String[] args) {
Random rand = new Random();
//隨機(jī)生成[0,1)之間的 double 類型的數(shù)據(jù)
System.out.println(rand.nextDouble());
//隨機(jī)生成 int 類型允許范圍之內(nèi)的整型數(shù)據(jù)
System.out.println(rand.nextInt());
//隨機(jī)生成[0,1)之間的 float 類型的數(shù)據(jù)
System.out.println(rand.nextFloat());
//隨機(jī)生成 false 或者 true
System.out.println(rand.nextBoolean());
//隨機(jī)生成[0,10)之間的 int 類型的數(shù)據(jù)
System.out.print(rand.nextInt(10));
//隨機(jī)生成[20,30)之間的 int 類型的數(shù)據(jù)
System.out.print(20 + rand.nextInt(10));
//隨機(jī)生成[20,30)之間的 int 類型的數(shù)據(jù)(此種方法計(jì)算較為復(fù)雜)
System.out.print(20 + (int) (rand.nextDouble() * 10));
}
}

五、File 類
java.io.File 類:代表文件和目錄。 在開發(fā)中,讀取文件、生成文件、刪除文件、修改文件的屬性時(shí)經(jīng)常會用到本類。
1. 使用File類創(chuàng)建文件
public class TestFile1 {
public static void main(String[] args) throws Exception {
System.out.println(System.getProperty("user.dir"));
File f = new File("a.txt"); //相對路徑:默認(rèn)放到 user.dir 目錄下面
f.createNewFile();
//創(chuàng)建文件
File f2 = new File("d:/b.txt");
//絕對路徑
f2.createNewFile();
}
}
2. 使用File類訪問文件或目錄的屬性
public class TestFile2 {
public static void main(String[] args) throws Exception {
File f = new File("d:/b.txt");
System.out.println("File 是否存在:" + f.exists());
System.out.println("File 是否是目錄:" + f.isDirectory());
System.out.println("File 是否是文件:" + f.isFile());
System.out.println("File 最后修改時(shí)間:" + new Date(f.lastModified()));
System.out.println("File 的大?。? + f.length());
System.out.println("File 的文件名:" + f.getName());
System.out.println("File 的目錄路徑:" + f.getPath());
}
}

3. 創(chuàng)建文件夾
mkdir:創(chuàng)建單個(gè)文件夾,如果創(chuàng)建路徑中,目錄結(jié)構(gòu)中有一個(gè)不存在,則不會創(chuàng)建整個(gè)目錄樹
mkdirs:創(chuàng)建多級目錄,目錄結(jié)構(gòu)中有一個(gè)不存在也沒關(guān)系;創(chuàng)建整個(gè)目錄。
4. 遞歸遍歷和樹狀展示目錄樹
class TestFile6 {
public static void main(String[] args) {
File f = new File("d:/DDU");
printFile(f, 0);
}
/**
* 打印文件信息
* @param file 文件名稱
* @param level 層次數(shù)(實(shí)際就是:第幾次遞歸調(diào)用)
*/
static void printFile(File file, int level) {
//輸出層次數(shù)
for (int i = 0; i < level; i++) {
System.out.print("-");
}
// 輸出文件名
System.out.println(file.getName());
//如果 file 是目錄,則獲取子文件列表,并對每個(gè)子文件進(jìn)行相同的操作
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File temp : files) { //遞歸調(diào)用該方法:注意等+1
printFile(temp, level + 1);
}
}
}
}

六、枚舉
枚舉類,也是一種類,是表示一種事物的所有情況,這個(gè)類型的實(shí)例有枚舉類自己控制和提供。
enum 枚舉名 {
枚舉體(常量列表)
}
1. 創(chuàng)建枚舉類
enum Season {
SPRING, SUMMER, AUTUMN, WINDER
}
所有的枚舉類型隱性地繼承自 java.lang.Enum。枚舉實(shí)質(zhì)上還是類!而每個(gè)被枚舉的成員實(shí)質(zhì)就是一個(gè)枚舉類型的實(shí)例,他們默認(rèn)都是 public static final 修飾的。可以直接通 過枚舉類型名使用它們。
2. 枚舉類的使用
public class TestEnum {
public static void main(String[] args) { // 枚舉遍歷
for (Week k : Week.values()) {
System.out.println(k);
}
// switch 語句中使用枚舉
int a = new Random().nextInt(4); // 生成 0,1,2,3 的隨機(jī)數(shù)
switch (Season.values()[a]) {
case SPRING:
System.out.println("春天");
break;
case SUMMER:
System.out.println("夏天");
break;
case AUTUMN:
System.out.println("秋天");
break;
case WINDTER:
System.out.println("冬天");
break;
}
}
}
/**
* 季節(jié)
*/
enum Season {SPRING, SUMMER, AUTUMN, WINDTER}
/**
* 星期
*/
enum Week {星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期日}