java-lang
java.lang包包含著Java最基礎(chǔ)和核心的類,在編譯時(shí)會(huì)自動(dòng)導(dǎo)入。它提供了java中基礎(chǔ)類。常用的有Object類、String類、基本類型的包裝類、Math計(jì)算類等等。
包的位置:%JAVA_HOME%/jre/lib/rt.jar
包裝類:
每個(gè)基本類型在java.lang包中都有一個(gè)相應(yīng)的包裝類
包裝類的作用:
集合不允許存放基本類型數(shù)據(jù),存放數(shù)字時(shí),要用包裝類型
包裝類的構(gòu)造方法
1.所有包裝類都可以將與之對(duì)應(yīng)的基本數(shù)據(jù)類型作為參數(shù),來構(gòu)造他們的實(shí)例
如:Integer i = new Integer(1);
2.處character類外,其他包裝類可將一個(gè)字符串作為參數(shù)構(gòu)造它們的實(shí)例
如:Integer i = new Integer("123");
注意事項(xiàng):
1.Boolean類構(gòu)造方法參數(shù)為String類型時(shí),若該字符串內(nèi)容為true(不考慮大小寫),則該Boolean對(duì)象表示true,否則表示false。(內(nèi)部比較時(shí)用的是equalsIgnoreCase)
Boolean boolean1 = new Boolean("TRUE");
2.當(dāng)Number包裝類構(gòu)造方法參數(shù)為String類型時(shí),字符串不能為null,且該字符串必須可以解析為相應(yīng)的基本數(shù)據(jù)類型的數(shù)據(jù),否則編譯通過,運(yùn)行時(shí)NumberFormatException(數(shù)字格式)異常
Integer integer = new Integer("null");
自動(dòng)拆裝箱:
也解決了集合中不能放基本類型的問題
什么時(shí)候自動(dòng)裝箱:
Integer i =100;
相當(dāng)于編譯器自動(dòng)為您作以下的語法編譯:Integer i = Integer.valueOf(100);
什么時(shí)候自動(dòng)拆箱:
自動(dòng)拆箱(unboxing),也就是將對(duì)象中的基本數(shù)據(jù)從對(duì)象中自動(dòng)取出
Integer i = 10; //裝箱
int t = i; //拆箱,實(shí)際上執(zhí)行了 int t = i.intValue();
Integer的自動(dòng)裝箱
//在-128~127 之外的數(shù)
Integer i1 = 200;
Integer i2 = 200;
System.out.println("i1==i2: " + (i1 == i2));
// 在-128~127 之內(nèi)的數(shù)
Integer i3 = 100;
Integer i4 = 100;
System.out.println("i3==i4: " + (i3 == i4));
輸出的結(jié)果是:
i1==i2: false
i3==i4: true
equals() 比較的是兩個(gè)對(duì)象的值(內(nèi)容)是否相同。
"==" 比較的是兩個(gè)對(duì)象的引用(內(nèi)存地址)是否相同,
int的自動(dòng)裝箱,是系統(tǒng)執(zhí)行了Integer.valueOf(int i),源碼:
public static Integer valueOf(int i) {
if(i >= -128 && i <= IntegerCache.high) //默認(rèn)是127
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
對(duì)于–128到127(默認(rèn)是127)之間的值,Integer.valueOf(int i) 返回的是緩存的Integer對(duì)象(并不是新建對(duì)象)
所以范例中,i3 與 i4實(shí)際上是指向同一個(gè)對(duì)象。
而其他值,執(zhí)行Integer.valueOf(int i) 返回的是一個(gè)新建的 Integer對(duì)象,所以范例中,i1與i2 指向的是不同的對(duì)象。
Math類
floor: 取整,返回小于目標(biāo)數(shù)的最大整數(shù)
ceil:取整,返回大于目標(biāo)數(shù)的最小整數(shù)
round:四舍五入取整
System.out.println("Math.floor(1.2):" + Math.floor(1.2));//1.0
System.out.println("Math.ceil(1.2):" + Math.ceil(1.2));//2.0
System.out.println("Math.round(2.3):" + Math.round(2.3));//2
三角運(yùn)算(正弦、余弦、正切等)
異常
一種嚴(yán)重的:Error,一種不太嚴(yán)重的:Exception
對(duì)于Error,一般不寫針對(duì)性的代碼進(jìn)行處理
對(duì)于不太嚴(yán)重的,java對(duì)于Exception可以通過針對(duì)性的方式進(jìn)行處理
特殊情況:try對(duì)應(yīng)多個(gè)catch時(shí),如果有父類的catch語句塊,一定要放在下面。
java.lang.Throwable:
Throwable
| --- Error
| --- Exception
try、catch、finally使用:
try{}用來定義需要被檢測的代碼,catch(){}用來處理異常的代碼,finally{}中是一定會(huì)執(zhí)行的代碼
注意:
finally中定義的通常是 關(guān)閉資源代碼。因?yàn)橘Y源必須釋放。當(dāng)執(zhí)行到System.exit(0)時(shí),finally不會(huì)執(zhí)行。
JDK1.7異常處理新特性:
多個(gè)catch用一個(gè)catch替代
格式:
catch(異常1|異常2|異常3...變量名)
輸出結(jié)果?
public class Testyc {
public static void main(String[] args) {
try {
int i = 1/0;
} catch (Exception e) {
System.out.println("catch");
return;
}finally{
System.out.println("finally");
}
}
}
catch
finally
catch里面有return語句,finally里面的語句還會(huì)執(zhí)行,在return前執(zhí)行。
線程
定義線程
1.繼承java.lang.Thread類
此類重寫Thread類的run()方法。
public class TestxcThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(getName()+":"+i);
}
}
}
public class TestThread {
public static void main(String[] args) {
TestxcThread xc1 = new TestxcThread();
TestxcThread xc2 = new TestxcThread();
xc1.setName("線程1");
xc2.setName("線程2");
xc1.start();
xc2.start();
}
}
開啟線程:start(),這個(gè)方法其實(shí)做了兩件事情,第一,讓線程啟動(dòng),第二,自動(dòng)調(diào)用run()方法
2.實(shí)現(xiàn)java.lang.Runnable接口
重寫run()方法
public class TestxcThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(getName()+":"+i);
}
}
}
public class TestRunnable {
public static void main(String[] args) {
TestxcRunnable test1 = new TestxcRunnable();
TestxcRunnable test2 = new TestxcRunnable();
Thread xc1 = new Thread(test1);
Thread xc2 = new Thread(test2);
xc1.setName("線程1");
xc2.setName("線程2");
xc1.start();
xc2.start();
}
}
//start->start0(調(diào)用 run)->target(new Tread 時(shí)會(huì)把target附上值,會(huì)執(zhí)行test1.run();
public void run() {
if (target != null) {
target.run();
}
}
線程生命周期
線程的生命周期,把圖轉(zhuǎn)化為文字就是:
線程通過new方法創(chuàng)建,調(diào)用start()方法,線程進(jìn)入就緒狀態(tài),等待系統(tǒng)的調(diào)度。當(dāng)系統(tǒng)調(diào)度,進(jìn)入運(yùn)行狀態(tài)。正常結(jié)束或者異常退出,進(jìn)程進(jìn)入死亡狀態(tài)。
處于運(yùn)行狀態(tài)的線程yieId()方法,線程轉(zhuǎn)為就緒狀態(tài)。
處于運(yùn)行狀態(tài)的線程遇到wait()方法,線程處于等待狀態(tài),需要notify()/notifyAll()來喚醒線程,喚醒后的線程處于鎖定狀態(tài),獲取了“同步鎖”之后,線程才轉(zhuǎn)為就緒狀態(tài)。
wait()、notify()、notifyAll()
wait()、notify()、notifyAll()是三個(gè)定義在Object類里的方法,可以用來控制線程的狀態(tài)。
這三個(gè)方法最終調(diào)用的都是jvm級(jí)的native方法。隨著jvm運(yùn)行平臺(tái)的不同可能有些許差異。
如果對(duì)象調(diào)用了wait方法就會(huì)使持有該對(duì)象的線程把該對(duì)象的控制權(quán)交出去,然后處于等待狀態(tài)。
如果對(duì)象調(diào)用了notify方法就會(huì)通知某個(gè)正在等待這個(gè)對(duì)象的控制權(quán)的線程可以繼續(xù)運(yùn)行。
如果對(duì)象調(diào)用了notifyAll方法就會(huì)通知所有等待這個(gè)對(duì)象控制權(quán)的線程繼續(xù)運(yùn)行。
其中wait方法有三個(gè)方法:
wait()
wait(long)
wait(long,int)
wait方法通過參數(shù)可以指定等待的時(shí)長。如果沒有指定參數(shù),默認(rèn)一直等待直到被通知。
wait和sleep的區(qū)別
分析這兩個(gè)方法:從執(zhí)行權(quán)和鎖上來分析:
wait:可以指定時(shí)間也可以不指定時(shí)間。不指定時(shí)間,只能由對(duì)應(yīng)的notify或者notifyAll來喚醒。
sleep:必須指定時(shí)間,時(shí)間到自動(dòng)從凍結(jié)狀態(tài)轉(zhuǎn)成運(yùn)行狀態(tài)(臨時(shí)阻塞狀態(tài))。
wait:線程會(huì)釋放執(zhí)行權(quán),而且線程會(huì)釋放鎖。
Sleep:線程會(huì)釋放執(zhí)行權(quán),但不是不釋放鎖。
commons-lang
Apache Commons lang包提供了標(biāo)準(zhǔn)Java庫函數(shù)里沒有提供的Java核心類的操作方法。Apache Commons Lang為java.lang API提供了大量的輔助工具,尤其是在String操作方法。
StringUtils
如何判斷字符串為空的情況?
String a=null;
if(a!=null && a!=""){}
需要判斷的地方有很多,代碼累贅
StringUtils : 該類主要提供對(duì)字符串的操作,對(duì)null是安全的,主要提供了字符串查找,替換,分割,去空白,去掉非法字符等等操作
// 1.public static boolean isEmpty(String str)
// 判斷某字符串是否為空,為空的標(biāo)準(zhǔn)是str == null 或 str.length() == 0
// 下面是示例:
System.out.println(StringUtils.isEmpty(null));// true
System.out.println(StringUtils.isEmpty(""));// true
System.out.println(StringUtils.isEmpty(" "));// false
System.out.println(StringUtils.isEmpty(" "));// false
System.out.println(StringUtils.isEmpty("bob"));// false
System.out.println(StringUtils.isEmpty(" bob "));// false
// 2.public static boolean isNotEmpty(String str)
//判斷某字符串是否非空,等于!isEmpty(String str)
//下面是示例:
System.out.println(StringUtils.isNotEmpty(null));// false
System.out.println(StringUtils.isNotEmpty(""));// false
System.out.println(StringUtils.isNotEmpty(" "));// true
System.out.println(StringUtils.isNotEmpty(" "));// true
System.out.println(StringUtils.isNotEmpty("bob"));// true
System.out.println(StringUtils.isNotEmpty(" bob "));// true
//3. public static boolean isBlank(String str)
//判斷某字符串是否為空或長度為0或由空白符(whitespace)構(gòu)成
///下面是示例:
StringUtils.isBlank(null);// true
StringUtils.isBlank("");// true
StringUtils.isBlank(" ");// true
StringUtils.isBlank(" ");// true
System.out.println(StringUtils.isBlank("\t \n \f \r"));// true
System.out.println(StringUtils.isBlank("\b"));// false
System.out.println(StringUtils.isBlank(" bob "));// false
//4. public static boolean isNotBlank(String str)
//判斷某字符串是否不為空且長度不為0且不由空白符(whitespace)構(gòu)成,
//等于!isBlank(String str)
// 下面是示例:
System.out.println(StringUtils.isNotBlank(null)); // false
System.out.println(StringUtils.isNotBlank("")); // false
System.out.println(StringUtils.isNotBlank(" ")); // false
System.out.println(StringUtils.isNotBlank(" ")); //false
System.out.println(StringUtils.isNotBlank("\t \n \f \r")); // false
System.out.println(StringUtils.isNotBlank("\b")); // true
System.out.println(StringUtils.isNotBlank("bob")); // true
System.out.println(StringUtils.isNotBlank(" bob ")); // true
//5. public static String trim(String str)
//去掉字符串兩端的控制符(control characters, char <= 32)
//如果輸入為null則返回null
//下面是示例:
System.out.println(StringUtils.trim(null)); // null
System.out.println(StringUtils.trim("")); // ""
System.out.println(StringUtils.trim(" ")); // ""
System.out.println(StringUtils.trim(" \b \t \n \f \r ")); // ""
System.out.println(StringUtils.trim(" \n\tss \b")); // "ss"
System.out.println(StringUtils.trim(" d d dd ")); // "d d dd"
System.out.println(StringUtils.trim("dd ")); // "dd"
System.out.println(StringUtils.trim(" dd ")); // "dd"
//6.public static String trimToNull(String str)
//去掉字符串兩端的控制符
//如果變?yōu)閚ull或"",則返回null
//下面是示例:
StringUtils.trimToNull(null); // null
StringUtils.trimToNull(""); // null
StringUtils.trimToNull(" "); // null
StringUtils.trimToNull(" \b \t \n \f \r "); // null
StringUtils.trimToNull(" \n\tss \b"); // "ss"
StringUtils.trimToNull(" d d dd "); // "d d dd"
StringUtils.trimToNull("dd "); // "dd"
StringUtils.trimToNull(" dd "); // "dd"
System.out.println(StringUtils.contains("defg", "ef"));//檢查一字符串是否包含另一字符串.
System.out.println(StringUtils.containsOnly("ef", "defg"));//檢查一字符串是否為另一字符串的子集
System.out.println("去除字符中的空格.");
System.out.println(StringUtils.deleteWhitespace("aa bb cc"));//aabbcc
System.out.println("分隔符處理成數(shù)組.");
String[] strArray = StringUtils.split("a,b,,c,d,null,e", ",");
System.out.println(strArray.length);//6
System.out.println("縮短到某長度,用...結(jié)尾.");
System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 10));//The qui...
System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 15, 10));//... fox...
ArrayUtils
ArrayUtils 提供了數(shù)組的復(fù)制,查找,獲取子數(shù)組,反轉(zhuǎn)等功
//判斷數(shù)組是否為空(null和length=0的時(shí)候都為空)
ArrayUtils.isEmpty(new int[0]);// true
ArrayUtils.isEmpty(new Object[] { null });// false
//合并兩個(gè)數(shù)組
ArrayUtils.addAll(new int[] { 1, 3, 5 }, new int[] { 2, 4 });// {1,3,5,2,4}
//刪除數(shù)組中某個(gè)位置上的數(shù)據(jù)
ArrayUtils.remove(new int[] { 1, 3, 5 }, 1);// {1,5}
// 刪除數(shù)組中某個(gè)對(duì)象(從正序開始搜索,刪除第一個(gè))
ArrayUtils.removeElement(new int[] { 1, 3, 5 }, 3);// {1,5}
//Null處理,如果輸入的兩個(gè)數(shù)組都為null時(shí)候則返回true
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, null);// false
ArrayUtils.isEquals(null, null);// true
//查詢某個(gè)Object是否在數(shù)組中
ArrayUtils.contains(new int[] { 3, 1, 2 }, 1);// true
//輸出數(shù)組中的元素內(nèi)容
ArrayUtils.toString(new int[] { 1, 4, 2, 3 });// {1,4,2,3}
ArrayUtils.toString(new Integer[] { 1, 4, 2, 3 });// {1,4,2,3}
ArrayUtils.toString(null, "I'm nothing!");// I'm nothing!
DateUtils和DateFormatUtils
DateUtils 主要提供了對(duì)日期的操作,包括日期加減,日期格式化,日期比較等。它們?cè)趏rg.apache.commons.lang.time包下。
1.與SUN的SimpleDateFormat相比 ,其主要優(yōu)點(diǎn)是:線程安全。
2.對(duì)應(yīng)于SimpleDateFormat的format()的方法,是DateFormatUtils 的format系列方法,常用的就是:
public static String format(Date date, String pattern)
3.對(duì)應(yīng)與SimpleDateFormat的parse()的方法,是DateUtils的parseDate方法,即:
public static Date parseDate(String dateValue) throws DateParseException
4.日期舍入與截整,DateUtils的truncate()方法可以將日期按照任意范圍截整,關(guān)鍵看第二個(gè)參數(shù)。
public static Date truncate(Date date, int field)
第二個(gè)參數(shù)取自Calendar的常量,可以是MONTH、DATE、HOUR等多種;
5.判斷是否是同一天,DateUtils的isSameDay()方法
public static boolean isSameDay(Date date1, Date date2)
6.DateFormatUtils定義了很多內(nèi)置的固定日期格式,均為FastDateFormat類型,比如 ISO_DATE_FORMAT。使用 FastDateFormat的format()方法可以直接將日期格式化為內(nèi)置的固定格式。
public String format(Date date)
//常用日期格式的格式化操作:
//以 yyyy-MM-dd 格式化:
System.out.println(DateFormatUtils.ISO_DATE_FORMAT.format(new Date()));//2017-06-17
//以 yyyy-MM-dd'T'HH:mm:ss 格式化:
System.out.println(DateFormatUtils.ISO_DATETIME_FORMAT.format(new Date()));//2017-06-17T12:51:18
//以 HH:mm:ss 格式化:
System.out.println(DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(new Date()));//12:51:18
//自定義日期格式的格式化操作:
// 以 yyyy-MM-dd HH:mm:ss 格式化Date對(duì)象:
System.out.println(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));//2017-06-17 12:51:18
//以 yyyy-MM-dd HH:mm:ss 格式化Calendar對(duì)象:
System.out.println(DateFormatUtils.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"));//2017-06-17 12:51:18
//以 yyyy-MM-dd HH:mm:ss 格式化TimeInMillis:
System.out.println(DateFormatUtils.format(Calendar.getInstance().getTimeInMillis(), "yyyy-MM-dd HH:mm:ss"));//2017-06-17 12:51:18
BooleanUtils
BooleanUtils用來操作基礎(chǔ)布爾或者布爾對(duì)象,很多方法在工作中可以經(jīng)常使用,下面是該工具類的一個(gè)藍(lán)圖:
Boolean[] arr = new Boolean[]{true,true};
//一假即假
BooleanUtils.and(arr); // true
System.out.println(BooleanUtils.isTrue(Boolean.TRUE));//true
System.out.println(BooleanUtils.isTrue(Boolean.FALSE));//false
System.out.println(BooleanUtils.isTrue(null));//false
System.out.println(BooleanUtils.isNotTrue(Boolean.TRUE));//false
System.out.println(BooleanUtils.isNotTrue(Boolean.FALSE));//false
System.out.println(BooleanUtils.isNotTrue(null));//true
Apache Commons只是工具類庫,并不能解決所有的問題,但是可以提高代碼的效率。Commons的工具類還有很多,還是需要大家在工作中不斷慢慢的積累。