常用類
概述:
一、基本數據類型包裝類;
二、字元串相關類;
不可變字元序列:String
可變字元序列:StringBuffer、StringBulider
三、時間相關處理類:
Date
DateFormat、SimpleDateFormat
Calender
四、Math類
五、枚舉類
—————————————————————————————————————————————————————————————————-
一、基本數據類型的包裝類:
將基本數據類型封裝到一個類中,包含屬性和方法,方便對象操作,位於java.lang包中。
使用:在使用過程中會涉及到自動裝箱和自動拆箱
裝箱:將基本數據類型轉換成包裝類
拆箱:將包裝類轉換成基本數據類型
涉及的幾個常見面試題:
int、Integer、new Integer的比較 參考鏈接://blog.csdn.net/weixin_38405253/article/details/100099620
區分幾個概念:
1、Integer是int的包裝類,int是一種基本數據類型;
2、int默認值是0,Integer默認值是null;
3、Integer必須實例化後才可以使用,int可以直接使用;
4、Integer是對象的引用,當new一個Integer時,實際上是成生一個指針指向此對象;而int則是直接存儲數據;
a、兩個new Integer()比較,永遠是false 因為new生成了兩個新對象,記憶體地址不一樣;
b、Integer和new Integer()比較,永遠是false 因為Integer指向的是java常量池中的對象,而new Integer()指向的是堆中新建的對象,所以兩個記憶體地址不一樣;
c、兩個Integer對象比較,如果是-128~127之間為true,否則為false 因為:
Integer i =100時,會被編譯成 Integer i = Integer.valueof(100) 而java對Integer的valueof方法定義如下:


1 private static class IntegerCache { 2 static final int low = -128; //最小 3 static final int high; 4 static final Integer cache[]; 5 6 static { 7 // high value may be configured by property 8 int h = 127; //最大 9 String integerCacheHighPropValue = 10 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); 11 12 。。。。。。 13 14 public static Integer valueOf(int i) { 15 if (i >= IntegerCache.low && i <= IntegerCache.high) 16 return IntegerCache.cache[i + (-IntegerCache.low)]; 17 return new Integer(i); 18 }
valueof
d、int和Integer、new Integer()比較 ,只要值是相等的,結果就為true 因為Integer和new Integer()與int比較時,會自動拆箱為int,此時就是兩個int變數做比較。
Integer i =100; ==> int i = i.intValue();
二、字元串相關類:
jdk1.7之後 常量池 放置到 堆 空間之中。
字元串的使用:
1、創建
String str = “abc”;
String str2 = new String(“abc”);
兩種方式都可以用,第一種更常用。
2、字元串的本質
字元串的本質是字元數組或者叫做字元序列;
String類使用final修飾,不可以被繼承;
使用equals方法比較的是字元數組的每一個位置的值;
使用substring方法時,需要注意是左閉右開區間 eg:String a=”abcdefg” ; s.substring(3,5); ==> de
關於String的面試題:
1、以下程式碼分別創建了幾個對象?
String a = “123”; 首先會去常量池中找「123」,如果有則直接引用這個常量的記憶體地址,如果沒有則創建一個,然後引用;所以至少創建0個對象,最多創建1個對象。
String b = new String(“345”); 首先new的時候一定會在堆中創建一個對象,然後去常量池中查找是否有「345」這個常量,如果有則直接引用該常量的記憶體地址,否則先創建一個,然後引用;所以至少創建1個對象,最多創建2個對象。
2、判斷下面對象是否相等?
String a = “abc”;
String a1 = new String(“abc”);
String b = “def”;
String c = a+b;
String d = “abc”+”def”;
String e = (a+b).intern();
String f = a+”def”;
String g = getStr()+”def”;
public String getStr(){ return “abc”; }
System.out.println(a==a1);
System.out.println(c==d);
System.out.println(c==e);
System.out.println(c==f);
System.out.println(c==g);
答案:
false
true
true
false
false
結論:
能明確知道其結果的為true;
intern():先查看常量池中是否有該變數 ,如果有則直接引用;
String d = “abc”+”def”; 由於編譯器優化,位元組碼文件反編譯後會發現,會直接把”abc”+”def”優化成”abcdef”;
3、StringBuffer與StringBuilder為什麼是可變字元串?
都繼承AbstractStringBuilder類,繼承了父類的append方法,初始容量是16,若超過了會執行expandCapacity(int minimumCapacity)方法,擴大容量;接著走到getchars()方法中,
System.arraycopy(value,srcBegin,dst,dstBegin,srsBegin-srcEnd); 其中
value:要添加的字元串;
srcBegin:指定的字元數組dst要在value的哪個位置插入;
dst:要被插入的字元數組;
dstBegin:字元數組的起始位置;
srcEnd-srcBegin:value的長度。
append方法其實是System.arraycopy()方法實現的。參考鏈接://www.cnblogs.com/NYfor2018/p/10353257.html
4、String與StringBuffer和StringBuilder的區別?
String:不可變字元串;(用反射可以改變值)
StringBuffer:可變字元串,效率低,執行緒安全;
StringBulider:可變字元串,效率高,執行緒不安全;
三、時間相關處理類:
五、枚舉類:
對於什麼情況下用枚舉?
對一些值固定不變,例如性別,不是女就是男,一個星期七天,或者web請求的返回狀態,error為-1,success為0,未登錄9……
舉例:


1 package com.test.CommonClass; 2 3 public enum StatusDemo { 4 BreakFirst("milk"),Lunch("apple"),Supper("eggs"); 5 StatusDemo(String name){ 6 this.name = name; 7 } 8 private String name; 9 public void show(){ 10 System.out.println(name); 11 12 StatusDemo[] sd = StatusDemo.values(); 13 for (int i=0;i<sd.length;i++){ 14 System.out.println(sd[i].name); 15 } 16 } 17 }
StatusDemo


1 package com.test.CommonClass; 2 3 public class Test { 4 public static void main(String[] args) { 5 StatusDemo bf = StatusDemo.BreakFirst; 6 bf.show(); 7 System.out.println("----------------"); 8 System.out.println(StatusDemo.Lunch.name()); 9 } 10 }
Test
。。。。。。
持續更新中