Java教程01-基礎語法
阿新 • • 發佈:2019-01-14
目錄
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就是一個字串陣列,接收一系列命令列引數
}
}