jdk(java development kit),即Java開發工具集。java
jre(java runtime enviroment),即Java運行環境編程
jvm(java virtual machine),Java虛擬機。目前主要使用的式hotsopt。小程序
Green項目設計模式
應用環境:像電視盒這樣的消費類電子產品數組
要求:安全
發明者:James Gosling服務器
歷程:網絡
▪ 1991年,Sun公司的Green項目,Oak
▪ 1995年,推出Java測試版
▪ 1996年,JDK1.0
▪ 1997年,JDK1.1
▪ 1998年,JDK1.2,大大改進了早期版本的缺陷,是一個革命性的版本,改名爲Java2
▪ 1999 Java被分紅J2SE、J2EE 和J2ME,JSP/Servlet技術誕生
▪ 2004年,J2SE 5.0 (1.5.0) Tiger老虎.爲了表示這個版本的重要性,J2SE1.5改名爲J2SE5.0。
▪ 2006年,J2SE 6.0 (1.6.0) Mustang野馬.此時,Java的各類版本被改名,取消其中的數字"2":J2EE改名爲Java EE, J2SE改名爲
Java SE,J2ME改名爲Java ME
▪ 2009年4月20日甲骨文收購Sun公司,交易價格達74億美圓
▪ 2011年,JavaSE7.0
▪ 2014年 ,JavaSE8.0
▪ 2017年,JavaSE9.0
▪ 2018年3月,JavaSE10.0
▪ 2018年9月,JavaSE11.0
▪ 2019年3月,JavaSE12.0數據結構
public class Test{ public static void main(String[] args){ System.out.println("hello, world"); } }
用來給類、變量、方法、對象等起的名稱。由於在Java中一切皆對象,故不存在函數這一說,只有方法。多線程
首字母能夠是字母、下劃線、美圓符號,可是不能是數字
// 合法 int a; int _a; int A; int $a; // 不合法 int 1a;
其餘部分只能是字母、下劃線、數字、美圓符號
// 合法 int a1_$; // 不合法 int a-1;
Java嚴格區分大小寫
int a; int A; // a和A不是同一個變量
Java命名變量的時候建議採用駝峯標識以及變量要見名知意
// 建議 String name; int age; boolean isAlive; // 不建議 String s; int a; boolean b;
給系統使用的關鍵字,好比:int,float,void, class,for,while,break等等,在定義變量的時候不能定義成:int float;
。
算術運算符包括:+,-,*,/,%。
int a = 10; int b = 3; a + b; // 13 a - b; // 7 a * b; // 30 a / b; // 做商, 3 a % b; // 取餘, 1
比較運算符包括:==,!=,>,>=,<, <=。
int a = 10; int b = 3; a == b; // false a != b; // true a > b ; // true a >= b; // true a < b ; // false a <= b; // false
賦值運算符:int a = 10;
,將10賦值給變量a。
邏輯運算符包括:&&,||,!。
int a = 10; int b = 1; int c = 2; // && 兩邊同時爲true結果返回true,不然返回false (a > b) && (a > c); // true,a>b爲true,a>c爲true (a > b) && (a < c); // false, 由於,a>b爲true,a<c爲fasle // || 兩邊只要有一邊爲true,結果返回true (a > b) || (a > c); // true,a>b爲true,a>c爲true (a > b) || (a < c); // true,a>b爲true,a<c爲false (a < b) || (a > c); // true,a<b爲false,a>c爲true (a < b) || (a < c); // false,a<b爲fasle,a<c爲false // !,對取得的布爾值取反操做,!true->false, !false->true !(a>c); // fasle,a>c爲true // &&,|| 的短路操做 (a > b) && ((c = b) ==b); // true,c=1,由於a>b爲true,繼續比較右邊,執行右邊 (a < b) && ((c=b)==b); // false,c=2,由於a<b爲false,直接返回false,不比較後面代碼,也不執行後面代碼 (a > b) || ((c=b)==b); // true,c=2,由於a>b爲true,直接返回結果,不執行後面操做 (a < b) || ((c=b)==b); // true,c=1,由於a<b爲false,繼續比較右邊,執行右邊代碼,右邊結果爲true
形如:xxx?x:y。若xxx語句的返回值爲true,則返回x,不然返回y。
int a = 10>20?1:2; // a=2,由於10>20爲false
位運算符主要包括:&,|,^。
// & 將兩個數轉換爲二進制進行比較,相同的位上都是1,則該位比較後的值爲1 // | 將兩個數轉換爲二進制進行比較,相同的位上若是存在1,則該位比較後的值爲1 // ^ 將兩個數轉換爲二進制進行比較,相同的位上若是存在1,且不一樣時爲1,則該位比較後的值爲1 5轉換成二進制:0101 3轉換成二進制:0011 0101 & 0011 = 0001 轉換成10進制:1 0101 | 0011 = 0111 轉換成10進制:7 0101 ^ 0011 = 0110 轉換成10進制:6
byte b; short s; int i; long l; float f; double d; char c; boolean b; String string;
佔1位。
String
數組
定義方式
int[] numbers = new int[10]
;int[] numbers = new int[10]{1,2,3,4,5,6,7,8,9,0};
int[] numbers = {1,2,3,4,5,6,7,8,9,0};
使用
獲取
int[] numbers = {1,2,3}; System.out.println(numbers[1]);
修改
int[] numbers = {1,2,3}; numbers[1] = 10;
注意:
對象
類的實例化
class Person{ } Person person = new Person(); // person即爲對象
接口
某種行爲/功能的抽象,若要使用則要實現接口中的全部方法,接口相似於一種約束、規範
interface MyInterface{ void method(); }
等等
從上到下依次按順序執行
if
if(條件){ 執行語句 } // 但條件的返回結果爲true時執行執行語句,不然不執行 // 應用 int a = 10; if(a < 20){ a = 30; } // 由於 a<20爲true,執行語句,a=30
if...else
if(條件){ 執行語句 }else{ 執行語句2 } // 若條件的返回結果爲true,則執行語句,不然執行語句2 // 應用 int a = 10; if(a>10){ a = 8; }else{ a = 20; } // 由於a>10爲false,所以執行a=20
if...else if...else
if(條件1){ 執行語句1 }else if(條件2){ 執行語句2 }else{ 執行語句3 } // 若知足條件1,則執行語句1,不然若知足條件2,則執行語句2,不然執行語句3 // 應用 int a = 10; if(a > 10){ a = 8; }else if(a > 5){ a = 4 }else{ a = 100; } //a = 4,由於a>10爲false,a>5爲false,執行a=4
switch
switch(變量){ case value1: 執行語句1 break; case value2: 執行語句2 break; default: 執行語句3 break; } // 變量的值若是等於某一個case後面的值則執行相應的語句,而後跳出語句,不然就執行default裏的語句,而後退出 // 這裏變量以及case的值只能爲:整數、字符、字符串 // 應用 int a = 10; switch(a){ case 10: System.out.println("10"); break; case 20: System.out.println("20"); break; default: System.out.println("no"); break; } // 打印10
do...while
do{ 執行語句 }while(條件); // 循環執行語句,直到不知足條件,至少作一次 int a = 0; do{ System.out.print(i); i++; }while(i<20);
while
while(條件){ 執行語句 } // 但知足條件時循環執行語句,直到不知足條件 int a = 0; while(a < 10){ System.out.println(a); a++; }
for
for(初始化變量;知足的條件;更新變量){ 執行語句 } // 對於初始化變量,每次執行語句後更新變量,直到不知足條件 for(int a;a<10;a++){ System.out.println(a); }
加強for循環
對於一個集合、數組或者map,若其中的元素的類型都一致,則可使用使用forech(加強for循環來遍歷)
for(類型 變量:集合){ 執行語句 } // 應用 int[] numbers = {1,2,3,4,5}; for(int number:numbers){ System.out.println(number); }
break
跳出當前循環
for(int i=0;i<10;i++){ if(i==5){ break; } System.out.println(i); } //打印0-4後,更新變量,i=5,此時跳出for循環
continue
跳過本次循環
for(int i=0;i<10;i++){ if(i % 2 !=0){ continue; } System.out.println(i); } // 打印0-9全部的偶數
return
返回結果值
public int addOne(int number){ return number + 1; } System.out.println(addOne(10)); // 返回11
結束程序
for(int i=0;i<10;i++){ if(i ==5){ return; } } // 當i==5時,結束程序
典型的是C語言。經過分析出解決問題所須要的步驟,而後用函數把這些步驟一步一步實現,使用的時候一個一個依次調用就能夠了。以把大象放入冰箱爲例,咱們首先定義一個拿到大象的函數,而後定義一個開冰箱的函數,而後定義一個放大象的函數,而後定義一個關冰箱的函數,最後一次調用就能夠實現功能了。
在考慮問題時,以一個具體的流程(事務過程)爲單位,考慮它的實現。
典型的C++、C#、Java等編程語言。經過將現實世界中的物體抽象成一個具體的類,經過調用類的相關方法來完成一系列行爲。仍是以把大象放入冰箱爲例,咱們先定義一我的的類,有拿大象、放大象、開冰箱、關冰箱的方法(也就是人能夠作這些事情),再定義一個大象類,再定義一個冰箱類,而後建立相應的對象調用相關的方法便可。
在考慮問題時,以對象爲單位,考慮它的屬性及方法。
定義(規範,約束)與實現(名實分離的原則)的分離。
接口的自己反映了系統設計人員對系統的抽象理解。
經過預編譯方式和運行期間動態代理實現程序功能的統一維護的一種技術。AOP是OOP的延續,是軟件開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生範型。利用AOP能夠對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度下降,提升程序的可重用性,同時提升了開發的效率。
典型的是Python、JavaScript語言。
函數式編程是種編程方式,它將電腦運算視爲函數的計算。函數編程語言最重要的基礎是λ演算(lambda calculus),並且λ演算的函數能夠接受函數看成輸入(參數)和輸出(返回值)。主要思想是把運算過程儘可能寫成一系列嵌套的函數調用。
在現實生活中,人(Person)表示一種生物,很抽象,具體的人:張三,就是一個對象,一個具體的人,一個獨立的個體。
// 定義一個類 class Person{ } // 實例化一個對象(建立一個對象) Person zhangsan = new Person();
經過new關鍵字實現。
一個類的構造方法,用於對一個對象的初始化,好比說咱們在買電腦的時候都會選擇電腦的:cpu、內存、硬盤、顯示屏等等,這些至關於初始化。
class Computer{ private String cpu; private String arm; private String disk; private String screen; public Computer(String _cpu, String _arm, String _disk, String _screen){ cpu = _cpu; arm = _arm; disk = _disk; screen = _screen; } } // 利用構造器建立對象 Computer myComputer = new Computer("Intel", "英偉達","sanxing", "英偉達");
默認狀況下建議寫一個無參構造器。
指代當前對象。能夠在構造方法中使用,用來區分參數和成員變量,在普通方法中使用表示調用當前的屬性或者當前類的方法,能夠省略。
class Computer{ private String cpu; private String arm; private String disk; private String screen; public Computer(String cpu, String arm, String disk, String screen){ this.cpu = cpu; this.arm = arm; this.disk = disk; this.screen = screen; } } // 利用構造器建立對象 Computer myComputer = new Computer("Intel", "英偉達","sanxing", "英偉達");
表示靜態的,更準確的說是表示類的什麼什麼,被static修飾的方法、函數、類(內部類)在類加載的時候就建立了,而沒有被修飾的則是屬於對象,只有當建立對象的時候才建立,由於,static修飾的屬性、方法、類的建立是先於普通屬性、方法的,所以:靜態方法中不能存在普通的屬性、方法、對象。
被static修飾的屬性、方法,類能夠直接調用。
class Person{ static boolean isPerson = true; static void sleep(){ } } // 調用 System.out.println(Person.isPerson); System.out.println(Person.sleep());
表示最終的,不可更改,有三種形式
打包:區分同名的文件,分佈在不一樣的目錄。
將不一樣的功能代碼整合在一塊兒。
導包:方便引用其餘包的類
規定方法、類、屬性的訪問範圍。
分類:
在同一個類中,能夠定義多個同名方法,可是,這些方法必須知足如下三個條件之一:
參數類型不同
參數個數不同
參數順序不同
class Demo{ public void method(){ } public void method(String str){ } public void method(String str,int number){ } public void method(int number,String name){ } }
在代碼中能夠將某個代碼塊變成方法,抽象出某些工具類
public class Test2 { private String string; private int number; public String getString() { return string; } public void setString(String string) { this.string = string; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } }
解釋說明
重寫
super
public class Test2 extends Test4{ @Override public void method() { super.method1(); System.out.println("Test2->method"); } } class Test4{ public void method(){ System.out.println("Test4->method"); } public void method1(){ System.out.println("Test4->method1"); } }
public class Test2{ public static void main(String[] args) { personSpeak(new Boy()); // 傳入子類自動轉換成父類 personSpeak(new Girl()); personSpeak((Person) new Object()); // 父類到子類強制轉換,Object類是全部類的父類 } static void personSpeak(Person person){ person.speak(); } } abstract class Person{ abstract void speak(); } class Boy extends Person{ @Override void speak() { // 重寫父類方法 System.out.println("男孩說話"); } } class Girl extends Person{ @Override void speak() { System.out.println("女孩說話"); } }
public class Test2{ public static void main(String[] args) { new Singer().singSong(new Boy()); } } interface Sing{ final boolean isSing = true; void singSong(Person person); } class Singer extends Person implements Sing{ @Override public void singSong(Person person) { this.speak(); } @Override void speak() { } }
// 案例:人都要睡覺;人要說話,可是不一樣國家的人說的話不同,中國人說中文,美國人說美語 abstract class Person{ public void sleep(){ System.out.println("是我的都睡覺"); } abstract void speak(); } class Chinese extends Person{ @Override void speak() { System.out.println("中國人說做文"); } } class American extends Person{ @Override void speak() { System.out.println("美國人說美語"); } }
public class Demo1 { public static void main(String[] args) { // 靜態內部類 StaticInnerClass staticInnerClass = new Demo1.StaticInnerClass(); // 成員內部類 InnerClass innerClass = new Demo1().new InnerClass(); // 方法內部類,生命週期指在方法內 Object o = method(); // 匿名內部類 MyInterface myInterface = new MyInterface() {}; } static class StaticInnerClass{ } class InnerClass{ } public static Object method(){ class MethodInnerClass{ } return new MethodInnerClass(); } } interface MyInterface{ }
表示程序在運行過程當中出現的非正常狀況,編寫代碼的過程當中儘量少的減小異常出現的狀況
try...catch
try{ 可能出現異常的語句 }catch(可能出現的異常){ 捕捉異常後執行的語句 } // try{ int x = 1 /0; }catch(ArithmeticException e){ e.printStack(); }
try...catch...finally
try{ 可能發生異常的代碼 }catch(發生的異常){ 捕捉異常後執行的語句 }finally{ 無論是否發生異常都要執行的語句 } // try{ int x = 1 /0; }catch(ArithmeticException e){ e.printStack(); }finally{ System.out.println("finally") }
注意
當發生異常時,在try代碼塊中異常語句後面的語句不會再執行
try、catch、finally以及try...catch...finally以外的代碼是互相獨立的,在try中定義的變量在其餘的部分沒法訪問,改變了僅做用於try部分,其餘catch、finally狀況相似,可是外部定義的變量在try、catch、finally中可使用
在進行異常捕獲的時候,return語句的不一樣位置會形成結果的不一樣
首先討論try...catch...finally後面有代碼的狀況
//此時注意一個關鍵的點:checked異常,即return語句後面不能再跟語句 // 例如 return; System.out.println("after return");// 這條語句ide直接報錯,至關於多餘的 // 故return的語句只能有一下幾種狀況 //1 try{ System.out.println("1"); System.out.println(1 / 0); return; }catch (Exception e){ System.out.println("2"); // return; }finally { System.out.println("3"); // return; } System.out.println("4"); } // 此時return語句永遠都不會被執行,也就不會報錯。 //2 try{ System.out.println("1"); // System.out.println(1 / 0); return; }catch (Exception e){ System.out.println("2"); // return; }finally { System.out.println("3"); // return; } System.out.println("4"); //此時,當程序執行到return語句時,並不會當即執行,而是先掛起,等執行完finally中的語句以後再執行return語句結束程序,故try、catch、finally以外的其餘代碼不會再執行 //其餘的catch、finally中都不能再存在return語句了
try...catch...finally後面沒有代碼的狀況
//第一種狀況 try{ System.out.println("1"); System.out.println(1 / 0); return; }catch (Exception e){ System.out.println("2"); // return; }finally { System.out.println("3"); // return; } // 此時return語句永遠不會執行 // 第二種狀況,此時return語句有沒有都沒有關係 try{ System.out.println("1"); // System.out.println(1 / 0); return; }catch (Exception e){ System.out.println("2"); // return; }finally { System.out.println("3"); // return; } //第三種狀況 try{ System.out.println("1"); System.out.println(1 / 0); // return; }catch (Exception e){ System.out.println("2"); return; }finally { System.out.println("3"); // return; } //此時,仍是跟前面同樣的,return先掛起,執行完finally中的代碼後再結束程序 //第三種狀況 public class Demo { public static void main(String[] args) { System.out.println(method()); } public static int method(){ try{ System.out.println("1"); System.out.println(1 / 0); // return; }catch (Exception e){ System.out.println("2"); return 1; }finally { System.out.println("3"); // return 2; } // System.out.println("4"); return 0; } } // 此時會執行最近的return語句
綜上:
將異常狀況拋出給調用者
注意,若是整個程序的運行過程當中都沒有異常的處理的話,最終異常會拋給jvm,不太友好,通常都要對異常進行處理
import java.io.FileInputStream; import java.io.FileNotFoundException; public class Demo1 { public static void main(String[] args) throws FileNotFoundException { FileInputStream fileInputStream = new FileInputStream("test.txt"); } }
java中提供了很是多的異常類,可是在某些項目中須要本身定義獨特的異常處理方式,此時須要自定義異常
public class MyException extends Exception{ public MyException(){ System.out.println("自定義異常"); } } public class Demo1 { public static void main(String[] args) throws MyException { throw new MyException(); } }
另外,還能夠自定義異常信息
public class Demo1 { public static void main(String[] args) throws Exception { throw new Exception("test"); } }
基本數據類型都由對應的包裝類
自動拆箱和自動裝箱
舉例
public class Demo { public static void main(String[] args) { // 此時自動調用:Integer.valueOf(),若數字大於-128且小於127,直接返回值,不然新建一個對象 Integer integer1 = 1000; Integer integer2 = 1000; int i1 = 1000; int i2 = 1000; System.out.println(integer1 == integer2); // 由於兩個變量不是同一個對象所以結果爲false System.out.println(integer1.equals(integer2));// true System.out.println(i1 == i2); // true System.out.println(i1 == integer2); // true } }
不可變字符串:String
可變字符串:
舉例
public class Demo1 { public static void main(String[] args) { StringBuffer stringBuffer = new StringBuffer("hello,world"); // 增 stringBuffer.insert(0, "@"); stringBuffer.append("#"); // 刪 stringBuffer.delete(0,1); // 改 stringBuffer.setCharAt(0, '$'); stringBuffer.replace(0,1,"x"); // 查 stringBuffer.indexOf("a"); stringBuffer.substring(0,1); System.out.println(stringBuffer); } }
日期經常使用類
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class Demo2 { public static void main(String[] args) { DateFormat dateFormat = new SimpleDateFormat("YY-MM-dd HH-mm-ss"); // 建立格式化日期類 System.out.println(dateFormat.format(new Date())); } }
返回格林威治時間
import java.util.Calendar; import java.util.GregorianCalendar; public class Demo3 { public static void main(String[] args) { Calendar calendar = new GregorianCalendar(); System.out.println(calendar.getTime()); System.out.println(calendar.getTimeZone()); System.out.println(calendar.getFirstDayOfWeek()); System.out.println(calendar.getWeekYear()); } }
數學經常使用類
public class Demo4 { public static void main(String[] args) { System.out.println(Math.max(10,20)); System.out.println(Math.abs(-1)); System.out.println(Math.ceil(1.1)); System.out.println(Math.PI); System.out.println(Math.E); } }
import java.util.Random; public class Demo5 { public static void main(String[] args) { System.out.println(new Random().nextInt(10)); System.out.println(new Random().nextDouble()); } }
枚舉類也是一個類,能夠定義屬性和方法,甚至能夠寫構造方法,填寫具體的枚舉類對象的時候,能夠帶參數,可是要跟構造方法進行匹配
public class Demo6 { public static void main(String[] args) { System.out.println(Gender.FEMALE); } } public enum Gender { MALE,FEMALE; }
每一個枚舉的子枚舉均可以看做是枚舉的子類,能夠定義相關方法以及相應的set/get方法來獲取子枚舉攜帶的一些信息
public enum WEEK_DAY { MONDAY(0), TUESDAY(1), WEDNESDAY(2), THURSDAY(3), FRIDAY(4), SATURDAY(5), SUNDAY(6); private int value; public void method(int i){ System.out.println(i); } public int getValue(){ return this.value; } WEEK_DAY(int i) { this.value = i; } } public class Demo6 { public static void main(String[] args) { System.out.println(WEEK_DAY.FRIDAY.getValue()); } }
一個用來儲存數據的容器
形如:int[],String[]
的數據結構
特色:
具備的基本操做:
不惟一,有序
ArrayList實現了長度可變的數組,在內存中分配連續的空間
LinkedList採用鏈表存儲方式
LinkedList特有方法
採用Hashtable哈希表存儲結構
優勢:添加速度快,查詢速度快,刪除速度快
缺點:無序
LinkedHashSet
關鍵代碼
HashSet hs=new HashSet();//建立HashSet對象 hs.add(new Person("張三",20)); hs.add(new Person("李四",22)); hs.add(new Person("王五",23)); hs.add(new Person("李四",22));
@Override public int hashCode() { System.out.println(this.name+".....hashCode"); return 60; }
hashCode都相同,不符合實際狀況,繼續升級
修改hashCode方法
@Override public int hashCode() { System.out.println(this.name+".....hashCode"); return this.name.hashCode()+age; }
總結:
全部能夠「排序」的類都實現了java.lang.Comparable 接口 compareTo(Object obj) 方法。
該方法:
返回 0 表示 this == obj 返回正數 表示 this > obj 返回負數 表示 this < obj
實現了Comparable 接口的類經過實現 comparaTo 方法從而肯定該類對象的排序方式。
public class StrLenComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { if (o1.length()>o2.length()) { return 1; } if (o1.length()<o2.length()) { return -1; } return o1.compareTo(o2);//長度相同, 按字母 } } public static void sortDemo(){ List<String> list=new ArrayList<String>(); ..添加元素 sop(list); Collections.sort(list);//按字母排序 sop(list); //按照字符串長度排序 Collections.sort(list,new StrLenComparator()); sop(list); }
全部實現了Collection接口的容器類都有一個iterator方法用以返回一個實現了Iterator接口的對象。
Iterator對象稱做迭代器,用以方便的實現對容器內元素的遍歷操做。
Iterator接口定義了以下方法:
boolean hasNext(); //判斷是否有元素沒有被遍歷 Object next(); //返回遊標當前位置的元素並將遊標移動到下一個位置 void remove(); //刪除遊標左面的元素,在執行完next以後該 //操做只能執行一次
全部的集合類均未提供相應的遍歷方法,而是把遍歷交給迭代器完成。迭代器爲集合而生,與門實現集合遍歷
Iterator是迭代器設計模式的具體實現
Iterator方法:
可使用Iterator遍歷的本質 :實現Iterable接口
在迭代過程當中,準備添加或者刪除元素
ArrayList al=new ArrayList(); al.add("java1");//添加元素 al.add("java2"); al.add("java3"); //遍歷 Iterator it=al.iterator(); while(it.hasNext()){ Object obj=it.next(); if (obj.equals("java2")) { al.add("java9"); } sop("obj="+obj); }
在迭代時,不可能經過集合對象的方法(al.add(?))操做集合中的元素,會發生併發修改異常。 因此,在迭代時只能經過迭代器的方法操做元素,可是Iterator的方法是有限的,只能進行判斷(hasNext),取出(next),刪除(remove)的操做,若是想要在迭代的過程當中進行向集合中添加,修改元素等就須要使用 ListIterator接口中的方法
ListIterator li=al.listIterator(); while(li.hasNext()){ Object obj=li.next(); if ("java2".equals(obj)) { li.add("java9994"); li.set("java002"); } }
接口存儲一組鍵值對象,提供key到value的映射
Collections和Collection不一樣,前者是集合的操做類,後者是集合接口
Collections提供的靜態方法 :
數組不是面向對象的,存在明顯的缺陷,集合彌補了數組的一些缺點,比數組更靈活更實用,可大大提升軟件的開發效率,並且不一樣的集合框架類可適用不一樣場合。具體以下:
實現原理相同,功能相同,底層都是哈希表結構,查詢速度快,在不少狀況下能夠互用
二者的主要區別以下 :
流是指一連串流動的字符,是以先進先出方式發送信息的通道 。
XXX->程序-->輸入流
程序->XXX-->輸出流
輸入輸出流是相對於計算機內存來講的,而不是相對於源和目標
字節流是 8 位通用字節流,字符流是 16 位 Unicode 字符流
功能不一樣
節點流:能夠直接從數據源或目的地讀寫數據
處理流(包裝流):不直接鏈接到數據源或目的地,是其餘流進行封裝。目的主要是簡化操做和提升性能
節點流和處理流的關係
節點流處於io操做的第一線,全部操做必須經過他們進行
處理流能夠對其餘流進行處理(提升效率或操做靈活性)
用FileInputStream和FileOutputStream讀寫文本文件
使用FileInputStream 讀文本文件
引入相關的類
構造文件輸入流FileInputStream對象
讀取文本文件的數據
關閉文件流對象
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Demo { public static void main(String[] args) { FileInputStream fileInputStream; byte[] bytes = new byte[1024]; try { fileInputStream= new FileInputStream("test.txt"); fileInputStream.read(bytes); System.out.println(new String(bytes)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } fileInputStream.close(); } }
使用FileOutputStream 寫文本文件
引入相關的類
構造文件數據 流輸出對象
將數據寫入文 本文件
關閉文件流對象
import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; public class Demo2 { public static void main(String[] args) throws IOException { FileOutputStream fileOutputStream = new FileOutputStream("test.txt"); String string = "aaaaa"; fileOutputStream.write(string.getBytes(StandardCharsets.UTF_8)); fileOutputStream.close(); } }
用BufferedReader和BufferedWriter讀寫文本文件
使用 BufferedReader 讀文本文件
import java.io.*; public class Demo3 { public static void main(String[] args) throws IOException { FileReader fileReader = new FileReader("test.txt"); BufferedReader bufferedReader = new BufferedReader(fileReader); String result = ""; while ((result = bufferedReader.readLine())!=null){ System.out.println(result); } bufferedReader.close(); fileReader.close(); } }
使用 BufferedWriter 寫文件
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Demo4 { public static void main(String[] args) throws IOException { FileWriter fileWriter = new FileWriter("test.txt"); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); String string = "aaaaaaaa\naaaaaa\taaaa\nsdsdsds"; bufferedWriter.write(string); bufferedWriter.close(); fileWriter.close(); } }
使用DataInputStream和DataOutputStream讀寫二進制文件以及基本數據類型數據的讀寫
寫入數據
DataOutputStream dos=new DataOutputStream(new FileOutputStream("data.txt")); dos.writeInt(234); dos.writeBoolean(false); dos.writeDouble(9943.00); dos.writeUTF("中國"); dos.close();
讀取數據
DataInputStream dis=new DataInputStream(new FileInputStream("data.txt")); int num=dis.readInt(); boolean isFind=dis.readBoolean(); double price=dis.readDouble(); String str=dis.readUTF(); System.out.println(num+"\t"+isFind+"\t"+price+"\t"+str);
使用ObjectInputStream和ObjectOutputStream讀寫對象(序列化與反序列化)
注意:傳輸的對象須要實現Serializable接口
序列化
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("obj.txt")); oos.writeObject(new Person("張三",19)); oos.close();
反序列化
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("obj.txt")); Person p=(Person)ois.readObject(); System.out.println(p);
注意:
在Java中負責線程的這個功能的是Java.lang.Thread 這個類
能夠經過建立 Thread 的實例來建立新的線程
每一個線程都是經過某個特定Thread對象所對應的方法run( )來完成其操做的,方法run( )稱爲線程體
經過調用Thead類的start()方法來啓動一個線程
具體實現:
public class ThreadDemo01 extends Thread { //重寫父爲的run方法 @Override public void run() { for(int i=0;i<10;i++){ System.out.println("第"+i+"次threadrun........"); } } public static void main(String[] args) { //建立對象,就建立好一個線程 ThreadDemo01 d=new ThreadDemo01(); //d.run();//啓動線程使用start方法 d.start(); for(int i=0;i<5;i++){ System.out.println("main-->"+i); } } }
線程的執行
操做步驟
public class RunableDemo implements Runnable { @Override public void run() { for(int i=0;i<10;i++){ System.out.println("第"+i+"次threadrun........"); } } public static void main(String[] args) { //建立對象,就建立好一個線程 RunableDemo rd=new RunableDemo(); Thread t=new Thread(rd); t.start(); for(int i=0;i<5;i++){ System.out.println("main-->"+i); } } }
有三種方法能夠暫停Thread執行:
public class TicketImplements implements Runnable { private int tick=5; public void run() { while(true){ if (tick>0) { Thread.sleep(10); System.out.println(Thread.currentThread().getName()+"賣票:"+tick--); } } } public static void main(String[] args) { TicketImplements ti=new TicketImplements(); new Thread(ti).start(); new Thread(ti).start(); new Thread(ti).start(); new Thread(ti).start(); }
多線程的運行出現了安全問題
使用同步解決多線程的安全性問題
同步代碼塊
public void run() { while(true){ synchronized (this) {//一般將當前對象做爲同步對象 if (tick>0) { Thread.sleep(10); System.out.println(Thread.currentThread().getName()+"賣 票:"+tick--); } } } }
同步的前提
將須要同步的代碼放到方法中
public void run() { while(true){ sale(); } } public synchronized void sale(){ //一般將當前對象做爲同步對象 if (tick>0) { Thread.sleep(10); System.out.println(Thread.currentThread().getName()+"賣票:"+tick--); } }
同步監視器
同步監視器的執行過程
Java提供了3個方法解決線程之間的通訊問題
一組相互鏈接的計算機
OSI參考模式:開放系統互連參考模型(Open System Interconnect)
TCP/IP參考模型:傳輸控制/網際協議 Transfer Controln Protocol/Internet Protocol
得到百度主機名:
InetAddress ia2=InetAddress.getByName("www.baidu.com"); System.out.println("其它主機名稱:"+ia2.getHostAddress());
進行網絡通訊時, Socket須要藉助數據流來完成數據的傳遞工做
客戶端
Socket socket=new Socket("localhost",8800);
OutputStream os=socket.getOutputStream();
String info="用戶名: Tom;用戶密碼: 123456"; os.write(info.getBytes()); socket.shutdownOutput();
關閉全部的數據流和Socket
os.close(); socket.close();
服務端
創建鏈接,處理髮送到指定端口的數據
ServerSocket server=new ServerSocket(8800);
獲取客戶端對象
Socket socket=server.accept();
數據流中讀寫信息
InputStream is=socket.getInputStream(); byte[] buf=new byte[1024]; int len=is.read(buf); syso(new String(buf,0,len)) socket.shutdownInput();
關閉全部的數據流和Socket
is.close(); socket.close(); server.close()
序列化對象
User user=new User();//User是用戶類 user.setLoginName("Tom"); user.setPwd("123456"); oos.writeObject(user);
通訊雙發不須要創建鏈接 ,通訊雙方徹底平等
import java.io.IOException; import java.net.*; public class Client { public static void main(String[] args) throws IOException, InterruptedException { DatagramSocket datagramSocket = new DatagramSocket(new InetSocketAddress(InetAddress.getByName("localhost"),8888)); datagramSocket.connect(InetAddress.getByName("localhost"), 9999); ReadThread readThread = new ReadThread(datagramSocket); WriteThread writeThread = new WriteThread(datagramSocket); readThread.start(); writeThread.start(); } } public class Client2 { public static void main(String[] args) throws IOException, InterruptedException { DatagramSocket datagramSocket = new DatagramSocket(new InetSocketAddress(InetAddress.getByName("localhost"),9999)); datagramSocket.connect(InetAddress.getByName("localhost"), 8888); ReadThread readThread = new ReadThread(datagramSocket); WriteThread writeThread = new WriteThread(datagramSocket); readThread.start(); writeThread.start(); } } import java.nio.charset.StandardCharsets; public class ReadThread extends Thread{ private DatagramSocket datagramSocket; private byte[] bytes = new byte[1024]; private byte[] bytes1; DatagramPacket datagramPacket = new DatagramPacket(this.bytes, 0,1024); public ReadThread(DatagramSocket datagramSocket){ this.datagramSocket = datagramSocket; } @Override public void run() { super.run(); while (true){ try { this.datagramSocket.receive(this.datagramPacket); this.bytes1 = new String(this.bytes, 0, this.bytes.length).getBytes(StandardCharsets.UTF_8); System.out.println(new String(this.bytes1, 0, this.bytes1.length)); Thread.sleep(2); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } } import java.util.Scanner; public class WriteThread extends Thread{ private DatagramSocket datagramSocket; private String message = ""; private byte[] bytes = new byte[1024]; private Scanner scanner = new Scanner(System.in); public WriteThread(DatagramSocket datagramSocket){ this.datagramSocket = datagramSocket; } @Override public void run() { super.run(); while (true){ synchronized (WriteThread.class){ System.out.print("輸入:"); this.message = this.scanner.nextLine(); } DatagramPacket datagramPacket = null; try { datagramPacket = new DatagramPacket(message.getBytes(StandardCharsets.UTF_8),0, message.length(), new InetSocketAddress(InetAddress.getByName("localhost"),datagramSocket.getPort())); } catch (UnknownHostException e) { e.printStackTrace(); } try { datagramSocket.send(datagramPacket); } catch (IOException e) { e.printStackTrace(); }finally { try { Thread.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
Lambda表達式是 Java8 中最重要的新功能之一。使用 Lambda 表達式能夠替代只有一個抽象函數的接口實現,告別匿名內部類,代碼看起來更簡潔易懂。 Lambda表達式同時還提高了對集合、框架的迭代、遍歷、過濾數據的操做
任何有函數式接口的地方
只有一個抽象方法(Object類中的方法除外) 的接口是函數式接口
方法引用是用來直接訪問類或者實例的已經存在的方法或者構造方法,方法引用提供了一種引用而不執行方法的方式,若是抽象方法的實現剛好可使用調用另一個方法來實現,就有可能可使用方法引用