SE

1.字符串字面量在內容一樣的時候重用同一個String對象
字面量和常量的運算結果是字符串,也同用內存

String s1="123abc";
String s2="123abc";
syso(s1==s2)//true
String s1 = "1"+"23"+"abc";
String s2 = "1"+23+"abc";
String s3 = '1'+23+"abc";
String s4="123abc";
System.out.print(s1==s2);//true
System.out.print(s1==s3);//false
System.out.println(s1==s4);//false

2.String,StringBuilder,StringBuffer區別:操作量大的前提下,StringBuilder>StringBuffer>String
一般操作量少用String,StringBuilder非線程安全,StringBuffer線程安全
3.==和equals方法的區別:
對於基本類型的變量,==比較值,equals不能用於基本類型變量
對於引用類型的變量,在沒有重寫equals的前提下,都是比較對象的地址。String重寫了equals方法。
4.Object類的toString方法返回值是類名@散列碼,沒有實際意義,建議重寫
equals默認比較地址

    public String toString() {
        return x+","+y;
    }
    //重寫equals方法比較兩個對象是否相等
    public boolean equals(Object obj){
        //當兩個對象的x和y都相等時候則相等
        if(obj==null) return false;
        if(this==obj) return true;
        //使用if語句保護,避免造型異常
        if(obj instanceof PointX){
            //爲了讀取x y屬性必須造型爲子類型
            PointX other=(PointX)obj;
            return this.x == other.x && 
                this.y == other.y;
        }
        //方法一定返回一個boolean值!
        return false;
    }

5.包裝類:將基本類型包裝成對象;Java5後引入自動拆包裝
將字符串類型轉換爲基本類型

String str="-1232";
int i=Integer.parseInt(str);
System.out.println(i);//-1232
String s="Abc123";
int j=Integer.parseInt(s);
System.out.println(j);//NumberFormatExeception

6.時間API
Date類型對象中默認封裝了當前系統時間。
SimpleDateFormat:轉換爲自己想要的時間格式

SimpleDateFormat fmt=new SimpleDateFormat();
Date date=new date();
System.out.println();

將字符串按格式解析爲計算機時間

String str="1980-1-1";
SimpleDateFormat fmt=new SimpleDateFormat("yyyy-m-d");
Date date=fmt.parse(str);
System.out.println(fmt.format(date));

7.集合
Collection接口:
set接口:集合中的元素不按特定的方式排列,不能有重複的元素hashset實現類,treeset實現類;
list接口:集合中的元素以線性方式存儲,可以放重複元素。ArrayList實現類,可以對元素隨機訪問,向Array List插入和刪除元素較慢。本質上是數組;LinkedList實現類,對元素的訪問慢,插入和刪除元素快,本質上是鏈表;Vector實現類,線程安全的,採用數組。
contains方法:contains依賴於equals方法
集合的迭代:

Iterator ite=c.iterator();
while(ite.hasNext()){
    String s=(String)ite.next();
}

增強for循環:foreach(Object s: c)
Queue隊列
Java提供了Queue API,由LinkedList實現,用於處理先進先出的業務問題
1.offer將數據插入隊列2.peek檢查隊列頭部元素3.poll從隊列拉出數據
Stack棧
棧是後進先出的數據結構,Java利用Deque接口提供了棧操作方法push,pop由LinkedList實現了Stack結構:
push將數據入棧,pop將數據出棧

       // Deque 接口中定義了 棧操作方法
        // LinkedList 實現了棧操作方法
        Deque<String> stack = 
            new LinkedList<String>();
        //棧提供了 push方法可以將數據“壓”入棧中。
        stack.push("Tom");
        stack.push("Jerry");
        stack.push("Andy");
        //先壓入的數據在棧的最底部
        System.out.println(stack);
        //出棧的順序相反:後進先出
        while(! stack.isEmpty()){
            //利用pop可以從棧中彈出數據
            //彈出順序與進入順序相反
            String s = stack.pop();
            System.out.println(s); 
        }

查找表Map,key是否重複是看hashcode,String已經重寫hashcode,其他引用類型需要重寫hashcode和equals方法
hashmap:允許鍵和值爲空,非線程安全,效率較高,沒用contains方法
hashtable:不允許鍵和值爲空,線程安全,效率稍低,有contains方法。
hashmap是hashtable輕量級的實現,hashmap是Java1.2引入。
Map的常用實現類HashMap是查找性能最好的數據結構。
1.put(key,value),如果map中已存在key,則替換
2.value=get(key)從map中查找value
3.boolean containsKey(key)檢查map中是否包含key
關於hashcode:hashcode的存在主要是查找的快捷性;hashcode相同,並不表示地址相同,只能說明對象存在散列存儲結構中的同一個“籃子”;
4.map的遍歷:利用Entry遍歷集合,key的集合遍歷map

//利用key遍歷
Map<String,String> map=new HashMap<String,String>();
Set<String> keySet=map.keySet();//獲取全部key
foreach(String key:keySet){
    syso(key);
    syso(map.get(key));
}
//利用value遍歷
for(String values:map.values()){
}
//利用Entry遍歷map
Set<Entry<String,String>>entries=map.entrySet();
foreach(Entry<String,String> entry:entries){
    String key=entry.getKey();
    String value=entry.getValue();
}

8.Java File

        //使用絕對路徑創建文件夾
        //創建文件
        File file=new File("D:/demo");
        boolean a=file.mkdir();
        System.out.println(a);
        File file1=new File("D:/demo/test.txt");
        System.out.println("1");
        boolean b=file1.createNewFile();
        System.out.println("2");
        System.out.println(b);

9.IO流
IO流按照功能可以分爲兩大類:節點流和處理流
節點流:是流最原始的數據源,提供流最基本的功能。
處理流:也稱高級流
按照數據流向分爲輸入流和輸出流:inputStream,outputStream
9.1文件輸出入流FileOutputStream,FileInputStream
文件輸出流節點流,是以文件爲目標數據源的節點流,是基本的流,只提供基本的輸出方法write()輸入方法read()
9.2緩衝流(高級流)必須依賴基礎流,文件複製
*BufferedInputStream in=new BufferedInputStream(new FileInputStream(“D:/text.txt”));
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(“D:/test.txt”));
int b;
while((b=in.read())!=-1){
out.write(b);
}
in.close();
out.close();
**
9.3對象輸出流:高級流
實現序列化接口時候Java自動添加兩個方法:對象序列化方法(將對象變爲byte)
反序列化方法(將byte拼接成對象)
9.4字符流(高級流):只能處理文本文件

OutputStreamWriter osw=new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream()),"utf-8");
osw.write("hello");
osw.close();

InputStreamReader isr=new InputStreamReader(new BufferedInputStream(new FileInputStream("D:/test.txt")),"utf-8");
int c;
while((c=isr.read()!=-1)){  
    syso(c);
}

9.5PrintWriter寫文本文件,BufferedReader提供readLine

OutputStreamWriter osw=new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("D:/test.txt")),"utf-8");
PrintWriter pw=new PrintWriter(osw,true);//BufferedWriter br=new BufferedWriter(osw);
pw.print("gg");or pw.println()自動換行//bw.write();bw.newLine();

InputStreamReader isr=new InputStreamReader(new BufferedInputStream(new FileInputStrem("D:/test.txt")),"utf-8");
BufferedReader br=BufferedReader(isr,true);
String str;
while((str=br.readLine())!=null){
    syso(str);
}

10.異常Throwable
10.1分爲Error和Exception
10.2區別:Error表示系統出現比較嚴重的問題,與編寫的代碼和執行的操作無關,多數是JVM出現了問題。例如:棧空間不足,StackOverflowError,堆空間不足,OutofMemory;
Exeception表示不是特別嚴重的問題,可以預測和處理。分爲一般異常和運行時異常,運行時異常。一般異常需要try catch finally,運行時異常有:NullPointExeception,ClassNotFoundExeception,FileNotFoundExeception,NumberFormatExeceptionn,SQLExeception,ParseExeception,IndexOutOfBoundsExeception
11.線程
a.什麼是進程,什麼是線程
進程是操作系統中運行的一個任務,線程是操作系統能夠運算調度的最小單位,它被包含在進程中,是進程中實際的運作單位。
b.線程和進程的區別
一個進程有多個線程,多個線程共享進程的內存空間,線程是進程的子集。
c.實現線程的方法
繼承Thread類,實現runnable接口。因爲Java不支持多繼承,但可以實現多個接口,一般用runnable接口

//繼承Thread類
public static void main(String[]args){
    Thread1 t1=new Thread1();
    t1.start();
}
class Thread1 extends Thread{
    public void run(){
    }
} 
//實現Runnable接口
public static void main(String[]args){
    Run r=new Run();
    Thread t1=new Thread(r);
    t1.start();
}
class Run implements Runnable{
    public void run(){
    }
}
//匿名內部類
public static void main(String []args){
    Thread t1=new Thread(){
        public void run(){
        }
       };
         Runnable r=new Runnable(){
           public void run(){
           }
      };
    Thread t=new Thread(r);
    t.start();
}

d.線程的五種狀態,創建,就緒,運行,阻塞,和死亡
創建:生成線程對象,處於創建狀態;
就緒:調用strat()方法後,納入線程調度,線程處於就緒狀態,從等待或睡眠狀態回來之後會處於就緒狀態;
運行:線程調度將就緒狀態的線程設置爲當前線程,此時線程進入運行狀態,開始運行run方法中的代碼;
阻塞:線程運行的時候,被暫停。sleep(),suspend,wait()等都可能導致線程阻塞;
死亡:如果一個線程在run()方法結束,或者調用stop()方法,該線程就會死亡;
e.併發原理:多線程的同時運行只是我們感官上的一種表現,事實上線程是併發運行的,OS將時間劃分爲很多時間片段,儘可能的均勻分配給每個線程,獲取時間片段的線程被CPU執行,而其它線程等待,所以微觀上是走走停停,宏觀上是都在運行,這種現象叫併發。
多線程併發安全問題:當多個線程併發操作同一臨界資源時,由於線程切換時機不確定,可能導致多線程併發安全問題:synchronized
f.sleep和wait的區別:sleep是Thread的方法,導致此線程暫停執行指定時間,將執行機會給其它線程,但是監控狀態依然保持,到時自動恢復,sleep不會放棄對象鎖;wait是Object類的方法,導致此線程放棄對象鎖,必須針對此對象發出notify或者notiifyAll後此線程處於就緒狀態;
g.join():用於等待當前線程結束,纔會執行另一個線程。yeild()使當前線程主動讓出cpu時間片段,進入就緒狀態;
h.線程池作用:控制線程數量;重用線程;

/**
 * 獲取上週日日期
 *
 * @param n //n爲推遲的週數,-1向前推遲一週,0本週,1下週,依次類推
 * @return
 */
private String getPreSundayDate(int n) {
    Calendar cal = Calendar.getInstance(Locale.CHINA);
    cal.add(Calendar.DAY_OF_WEEK, n * 7);
    //想周幾,這裏就傳幾Calendar.SUNDAY(TUESDAY...)
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    cal.add(Calendar.WEEK_OF_YEAR, 1);
    return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章