1. 基本概念
1.1. 環境變量
環境變量通常是指在操作系統(win10,win7)中,用來指定操作系統運行時需要的一些參數(比如一個國家需要人民,貨幣,貨物...),一般為一些鍵值對

Path環境變量的作用->尋找命令
Path環境變量是操作系統外部命令搜索路徑

classpath變量的作用->尋找類文件

1.2. JDK里面有什么?

1.3. 什么是JRE?
JRE是JAVA運行的環境,包括以下幾個部分:
- Java虛擬機: 它是由一個軟件虛擬出來的計算機
- Java平台核心類文件
- 其他支持文件

2. Java的基礎
2.1. Java的寫法
// 類名與文件名必須一致
public class Main {
// Java虛擬機將從指定類的main方法執行
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
2.2. 變量和常量
2.2.1. Java的變量類型

2.2.2. Java的變量聲明
- 變量
int a = 10;
double salary = 12.23;
boolean done = true;
- 常量
public class Main {
// 類常量用static final定義
public static final double PRESEM = 2.54;
public static void main(String[] args) {
//基本常量用final定義
final int jbg = 12;
System.out.println("Hello World!");
}
}
2.3. Java的運算符
2.3.1. 數學函數
+、-、*、/Math.sqrt(x)/.pow(x,a)/.sin/.cos/.tan/.exp/.log/.log10/.PI/.E
2.3.2. 強制數值轉換
double x = 9.997;
//在圓括號中給出需要轉換的目標類型
int nx = (int) x;
2.3.3. 關系和boolean運算符
| 邏輯運算符 | 含義 |
|---|---|
&& |
and |
|| |
or |
!= |
not |
condition? exp1: exp2 |
如果condition為真,執行exp1 |
2.3.4. 枚舉類型
enum Size {SMALL, MEDIUM, LARGE};
Size s = Size.MEDIUM;
2.4. 字符串
String g = "Hello";
String w = "world";
// 子串
String s = g.substring(0,3);
//拼接
String m0 = g + w;
int val = 20;
String m1 = g + 20; //自動轉為string
String m2 = String.join("/", "S", "M", "L"); // 轉為S/M/L/XL
// 修改字符串: 注意: 字符串是不可變的
g = g.substring(0,3) + "p!"; // help!
// 檢測字符串是否相等
"Hello".equals(g);
"Hello".equalsIgnoreCase(g); //忽略大小寫
// 檢測字符串既不是null也不是空串
if (g!=null && g.length()!=0);
// 構建字符串
StringBuilder builder = new StringBuilder();
builder.append(g);
builder.apend(w);
String finishedmsg = builder.toString();
2.5. 輸入和輸出
2.5.1. 輸入
| 語句 | 含義 |
|---|---|
Scanner(InputStream in) |
用給定輸入流創建一個Scanner對象 |
String nextLine() |
讀取輸入的下一行內容 |
String next() |
讀取輸入的下一個單詞 |
int nextInt() |
讀取整數 |
double nextDouble() |
讀取浮點數 |
boolean hasNext() |
檢測是否還有其他單詞 |
boolean hasNextInt() |
其他整數 |
boolean hasNextDouble() |
其他浮點數 |
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("what is your name?");
String name = in.nextLine();
System.out.print("youge age?");
int age = in.nextInt();
System.out.println("your name is " + name + ". Your age is " + age);
}
}
2.5.2. 格式化輸出
System.out.println("Hello, %s, Your age is %d", name, age);
2.5.3. 文件的輸入輸出
public class Main {
public static void main(String[] args) throws IOException {
// 讀取文件內容
Scanner in = new Scanner(Paths.get("C:\\Users\\haoch\\Desktop\\Programming\\untitled\\test.txt"), "UTF-8");
System.out.println(in.nextLine());
// 將內容寫入文件
PrintWriter out = new PrintWriter("C:\\Users\\haoch\\Desktop\\Programming\\untitled\\test.txt", "UTF-8");
out.println("寫入內容");
}
}
2.6. Java的控制流程

2.7. 數組
數組是用來儲存同一數據類型的集合
public class Main {
public static void main(String[] args) throws IOException {
// 創建數組,指定數組長度(不可更改),創建后,所有元素初始化為0,布爾為false, 對象數組為null
int[] a = new int[10];
// 給數組賦值
for (int i=0; i<a.length; i++){
a[i] = i;
}
// foreach循環
for (int element: a){
System.out.println(element);
}
// 數組初始化(無需指定長度)
int[] b = {1,3,5,8,6,12};
// 數組拷貝(指向內存同一個區域)
int[] c = b;
int[] c_hard_copy = Arrays.copyOf(b, b.length);
// 數組排序
Arrays.sort(b);
// 命令行參數 在main方法中的String[] args就是一個字符串數組,接收一系列命令行參數
}
}
