三大版本:SE(我的)、ME、EE(企業)html
JDK:java開發者工具,包含JRE和JVM
JRE:java運行環境,包含JVM
JVM:java虛擬機(解釋器)java
一、jdk 8下載與安裝python
https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
卸載:刪除安裝目錄、註釋環境變量c++
二、環境變量配置算法
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home export PATH=$JAVA_HOME/bin:$PATH:. export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
三、驗證安裝api
java
javac
java -version數組
新建一個文件:Hello.java,寫入內容oracle
public class HelloWorld { public static void main(String[] args) { System.out.println("HelloWorld"); } }
執行命令ide
javac Hello.java # 編譯成class文件
java Hello # 執行代碼工具
編譯型:須要將代碼翻譯成其餘可執行文件,如c++、java;
解釋型:代碼可直接執行,如python;
編譯型相對來講性能較好;
public class Demo_01 { // psvm public static void main(String[] args) { int[] arrays = {1,2,3,4,5} // sout System.out.println(); // 100.for for (int i = 0; i < 100; i++) {} // arrays.for for (int array: arrays) {} // new Demo_01(); + option+Enter Demo_01 demo_01 = new Demo_01(); } }
// 單行註釋 command + / /* 多行註釋 option + command + / */ /** * 文檔註釋 */
標識符(命令規則)
一、不能用關鍵字命名
二、開頭只能是英文、$ 或下劃線 _
三、不能出現$ _ 以外的其餘符號
四、大小寫敏感
// java關鍵字:
abstract、assert、boolean、break、byte、case、catch、char、class、continue、default、do、double、else、enum、extends、final、finally、float、for、if、implements、import、int、interface、instanceof、long、native、new、package、private、protected、public、return、short、static、strictfp、super、switch、synchronized、this、throw、throws、transient、try、void、volatile、while
java是強類型語言,變量的定義必須先制定類型
基本類型:
byte:1個字節 2^8 = -128bit~127bit
short:2個字節 2^16= -32768~32767
int:4個字節 2^32=-2147483648~2147483647
long: 8個字節 2^64
float :4個字節
double: 8個字節
char:1個字節
boolean:1位 bit,True/False
注:1字節=8位
引用類型:
類、接口、數組
public class Demo_01 { public static void main(String[] args) { int num = 1000000; long num1 = 10000000000000L; // long 類型要在後面加L double num2 = 3.1415926535; char name = '中'; System.out.println(teacher); System.out.println(num); } }
// 二進制 0b 八進制0 十六進制 0x public class Demo_01 { public static void main(String[] args) { int i = 10; int j = 0b10000000; int k = 010; int l = 0x10; System.out.println(i); System.out.println(j); System.out.println(k); System.out.println(l); } }
public class Demo_01 { public static void main(String[] args) { int i = 10; byte j = (byte)i; // 強制轉換 高->低 int k = j; // 自動轉換 低->高 System.out.println(j); } } // 強制轉換注意內存溢出和精度問題
// 變量 public class Demo_01 { static double salary = 2500; // 類變量 int name; // 實例變量 public static void main(String[] args) { int i, j, k; // 局部變量 Demo_01 Demo = new Demo_01(); // 實例化類 int name = Demo.name; // 調用實例變量 double a = salary; // 調用類變量 } } /* 類變量:定義在類裏面 方法外面,加上static關鍵字 實例變量:定義在類裏面 方法外面,實例化類後調用 局部變量:定義在某方法裏面,僅供該方法使用 */
// 常量 使用final關鍵字 public class Demo_01 { static final double PI = 3.14; public static void main(String[] args) { System.out.println(PI); } }
太簡單不寫了
public class Demo_01 { public static void main(String[] args) { int a=0; int b = a++; // 先賦值再自增 System.out.println(a); // 1 System.out.println(b); // 0 } } public class Demo_01 { public static void main(String[] args) { int a=0; int b = ++a; // 先自增再賦值 System.out.println(a); // 1 System.out.println(b); // 1 } }
跟python的package差很少
通常用域名倒置來建包,如:com. baidu.www
java文件的第一句應該是: package xxx.xxx.xxx
導入第三方包:import xxx.xxx.xxx
/** * @author LinXin * @version 1.0.0 * @since 1.8.0 */ public class Demo_01 { /** * @throws Exception * @param a 形參 * @return a */ public String test(String a) throws Exception{ return a; } } // javadoc -encdeing UTF-8 -charset UTF-8 xxx.java // 生成幫助文檔
package:java.util.Scanner 獲取用戶輸入
語法:Scanner s = new Scanner(System.in);
api:next() nextLine() hasNext hasNextLine() hasNextInt ...
import java.util.Scanner; public class Demo_01 { public static void main(String[] args) { Scanner s = new Scanner(System.in); if (s.hasNext()) { // 若是有輸入 String str = s.next(); // 接收輸入,以空格爲結束 System.out.println(str); } s.close(); // 釋放對象 } } public class Demo_01 { public static void main(String[] args) { Scanner s = new Scanner(System.in); if (s.hasNextLine()) { // 若是有輸入 String str = s.nextLine(); // 接收輸入,以回車爲結束 System.out.println(str); } s.close(); // 釋放對象 } } public class Demo_01 { public static void main(String[] args) { Scanner s = new Scanner(System.in); if (s.hasNextInt()) { // 若是有輸入整數 int str = s.nextInt(); // 接收輸入,以回車爲結束 System.out.println(str); } else{ System.out.println("請輸入整數"); } s.close(); // 釋放對象 } }
從上到下執行
import java.util.Scanner; public class Demo_01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); // equals 判斷字符串是否相等 // 單選擇結構 if (s.equals("hello")){ System.out.println(s); } System.out.println("End"); scanner.close(); } } public class Demo_01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); // 雙選擇結構 if (score >= 60) { System.out.println("及格"); } else { System.out.println("不及格"); } System.out.println("End"); scanner.close(); } } public class Demo_01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); // 多選擇結構 if (score < 60) { System.out.println("不及格"); } else if (score < 70) { System.out.println("及格"); } else if (score < 80) { System.out.println("通常"); } else if (score < 90) { System.out.println("良"); } else if (score < 100) { System.out.println("優"); } else if (score == 100) { System.out.println("滿分"); } else { System.out.println("輸入錯誤"); } System.out.println("End"); scanner.close(); } } public class Demo_01 { public static void main(String[] args) { char grade = 'c'; // switch多選擇結構 匹配一個具體的值 // 不加break會出現case穿透現象 switch (grade){ case 'a': System.out.println("優"); break; case 'b': System.out.println("良"); break; case 'c': System.out.println("通常"); break; default: System.out.println("輸入錯誤"); } System.out.println("End"); } }
public class Demo_01 { public static void main(String[] args) { int i = 0; int sum = 0; while (i < 10) { // while 循環 先判斷再執行 i++; sum = sum + i; } System.out.println(sum); } } public class Demo_01 { public static void main(String[] args) { int i = 0; int sum = 0; do { // do while 循環 先執行後判斷 因此至少執行一次 i++; sum = sum + i; } while (i < 10); System.out.println(sum); } } public class Demo_01 { public static void main(String[] args) { int sum = 0; for (int i=1; i<=10; i++){ // for 循環 sum = sum + i; } System.out.println(sum); } } public class Demo_01 { public static void main(String[] args) { int[] list = {1, 2, 3, 4, 5}; // 定義數組 for (int x:list){ // 遍歷數組 System.out.println(x); } } }
設計原則:一個方法完成一個功能
public class Demo_01 { // main方法 public static void main(String[] args) { int sum = sum(1, 2); // 調用方法 System.out.println(sum); } // 自定義類方法(static), 類方法在該類中可直接調用 public static int sum(int a, int b) { // 返回1個int對象,接收2個int參數 return a + b; } }
在同一類下,方法名必須相同 且 形參必須不一樣(類型、數量、順序)的方法。做用是傳不一樣的實參,調不一樣的方法
public class Demo_01 { // main方法 public static void main(String[] args) { double sum = sum(1, 2); // 此時調用是第二個sum方法 System.out.println(sum); } public static int sum(int a, int b) { return a + b; } public static double sum(double a, double b) { return a + b; } }
public class Demo_01 { // main方法的args就是接收命令行參數的 public static void main(String[] args) { for (int i=0; i< args.length; i++){ System.out.println(args[i]); } } }
在方法的聲明中,指定參數參數類型後加一個 ... , 一個方法中只能指定一個可變參數,且必須是方法的最後一個參數
public class Demo_01 { // main方法 public static void main(String[] args) { Demo_01 demo = new Demo_01(); // 調用實例方法 demo.test('a', 2, 3, 4); // 傳參格式 } public void test(char a, int... number) { // 實際接收的是一個數組 for (int i = 0; i < number.length; i++) { System.out.println(number[i]); } } }
本身調用本身,結構包含2個部分:
public class Demo_01 { // main方法 public static void main(String[] args) { Demo_01 demo_01 = new Demo_01(); int f = demo_01.f(5); System.out.println(f); } public int f(int num) { if (num == 1) { // 遞歸結束條件 return 1; } else { return num * f(num - 1); // 遞歸調用自身 } } }
public class Demo_01 { public static void main(String[] args) { // 動態初始化:聲明變量、開闢空間、賦值 char[] chars = new char[10]; for (int i = 0; i < chars.length; i++) { chars[i] = '你'; // 給數組賦值 } System.out.println(chars); // 靜態初始化:聲明一個int類型的數組並開闢空間、賦值 int[] nums = {1, 2, 3, 4, 5}; for (int x:nums){ System.out.println(x); } } }
public class Demo_01 { public static void main(String[] args) { int[] arrays = {1, 2, 3, 4, 5}; int[] reversed = reversed(arrays); printArray(reversed); } public static int[] reversed(int[] arrays) { // 反轉數組 int[] res = new int[arrays.length]; for (int i = 0, j = res.length - 1; i < arrays.length; i++, j--) { res[j] = arrays[i]; } return res; } public static void printArray(int[] arrays) { // 打印數組 for (int i = 0; i < arrays.length; i++) { System.out.print(arrays[i] + " "); } } }
以二維數組爲例,int[2] [3] 能夠看做2行3列的矩陣
public class Demo_01 { public static void main(String[] args) { int[][] array = new int[3][4]; int[] a = {1, 2, 3, 0}; int[] b = {4, 5, 6, 0}; int[] c = {7, 8, 9, 0}; array[0] = a; array[1] = b; array[2] = c; // array = {{1, 2, 3, 0}, {4, 5, 6, 0}, {7, 8, 9, 0}} for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]); } System.out.println(); } } }
package:java.lang.Arrays
文檔:https://tool.oschina.net/apidocs/apidoc?api=jdk-zh
import java.util.Arrays; public class Demo_01 { public static void main(String[] args) { int[] a = {1, 5, 4, 3, 2}; System.out.println(a); // 打印hashcode,相似: [I@5cad8086 System.out.println(Arrays.toString(a)); // 打印整個數組 至關於python的 print([1, 2, 3, 4, 5]) Arrays.sort(a); // 正序 System.out.println(Arrays.toString(a)); Arrays.fill(a, 1,3,0); // 將下標爲[1,3)之間的元素值替換爲0 System.out.println(Arrays.toString(a)); } }
import java.util.Arrays; public class Demo_01 { public static void main(String[] args) { int[] array = {1,32,13,321,3,213,45,35,34,45,6546,54,7,6}; bubblingSort(array); } // 倒序 public static void bubblingSort(int[] array) { int temp; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array.length - i - 1; j++) { if (array[j + 1] > array[j]){ temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } System.out.println(Arrays.toString(array)); } }
當一個數組大部分元素爲0(沒有值),或值是一樣的時,能夠用稀疏數組
處理方式:
import java.util.Arrays; public class Demo_01 { public static void main(String[] args) { int[][] array_1 = new int[9][9]; array_1[0][0] = 1; array_1[1][1] = 2; array_1[2][2] = 3; // 輸出原始數組 for (int i = 0; i < array_1.length; i++) { System.out.println(Arrays.toString(array_1[i])); } // 計算出須要放到稀疏矩陣的元素個數 int sum = 0; for (int i = 0; i < array_1.length; i++) { for (int j = 0; j < array_1[i].length; j++) { if (array_1[i][j] != 0) { sum++; } } } System.out.println("======================================"); // 定義稀疏矩陣 int[][] array_2 = new int[sum + 1][3]; // sum行,3列 每行存一個元素信息,包括:行、列、值 array_2[0][0] = 9; // 第0行存放矩陣信息,分別爲: 原始行數、原始列數、存放元素個數 array_2[0][1] = 9; array_2[0][2] = sum; // 將元素存放到稀疏矩陣 int count = 1; for (int i = 0; i < array_1.length; i++) { for (int j = 0; j < array_1[i].length; j++) { if (array_1[i][j] != 0) { array_2[count][0] = i; array_2[count][1] = j; array_2[count][2] = array_1[i][j]; count++; } } } // 輸出稀疏矩陣 for (int i = 0; i < array_2.length; i++) { System.out.println(Arrays.toString(array_2[i])); } System.out.println("======================================="); //還原稀疏矩陣 int[][] array_3 = new int[array_2[0][0]][array_2[0][1]]; for (int i = 0; i < array_2.length - 1; i++) { array_3[array_2[i + 1][0]][array_2[i + 1][1]] = array_2[i + 1][2]; } // 輸出原始矩陣 for (int i = 0; i < array_3.length; i++) { System.out.println(Arrays.toString(array_3[i])); } } }