CoreJava 笔记总结-第三章 Java的基本程序设计结构
CoreJava 筆記總結
文章目錄
- CoreJava 筆記總結
- 第三章 Java的基本程序設計結構
- 數據類型
- 1. 整型
- 2. 浮點類型
- 3. char類型
- 4. boolean類型
 
- 變量與常量
- 1. 變量
- 2. 常量
- 3. 枚舉類型
 
- 運算符
- 關系和boolean運算符
- 數學函數與常量
- 強制類型轉換
- 字符串
- 空串與null串
- 碼點與代碼單元
- 構建字符串
 
 
- 輸入與輸出
- 讀取輸入
- 格式化輸出
- 文件輸入與輸出
 
- 控制流程
- 條件語句
- 循環
- 確定循環
- 多重選擇
- 中斷控制流程的語句
 
- 大數
- ==**數組**==
- 聲明數組
- 訪問數組元素
- ==for each循環==
- 數組拷貝
- 命令行參數
- 數組排序
- 多維數組
- 不規則數組
 
 
第三章 Java的基本程序設計結構
- punlic 訪問修飾符
- 類名: 以大寫字母開頭的名詞, 駱駝命名法
- 源代碼的文件名字必須與公共類名字相同
- main方法必須聲明為public
- 每個java應用程序都必須有一個main方法
- java通用語法: object.method(parameters)
- 注釋: /* xxx */ 或者 //xxx 或者 /** xxx */
數據類型
1. 整型
- int (20億左右 29) 4字節
- short 最大32767
- long
- byte -128~127
2. 浮點類型
-  float 有后綴f/F 如3.14F 
-  double雙精度, 3.14默認為雙精度, 也可以寫為3.14d 
-  正無窮負無窮NaN(不是一個數字) Double.POSITIVE_IFINITY Double.NEGATIVE_IFINITY Double.NaN 
3. char類型
- char類型字面量的值單引號括起來, e.g.'A'
4. boolean類型
- 布爾類型只有兩個值true, false
變量與常量
1. 變量
-  所有java語句以分號結束 
-  每個變量都有一個type: double, int... 
-  變量名: 一個由字母開頭, 并且由字母,數字構成的序列 
-  以下是合法聲明: 
-  - int i, j; Box box; Box aBox; //初始化 int vacationDay = 12; double salary = 6400.0
 
-  var: 對于局部變量, 該關鍵字可以代替其類型 
2. 常量
- final: 指示常量
- 常量名: 全部使用大寫
- 類常量:public static final double CM_PER_INCH = 2.54, 定義于main方法外部, public代表其他類也可以使用
3. 枚舉類型
- enum Size{SMALL, MIDDLE, LARGE, EXTRA_LARGE}
運算符
-  +, -, *, /, % 
-  如果public static strictfp void main(String[] args) , 那么這個類中所有方法使用嚴格的浮點計算 
-  +=, -=, *=, %=, ++, -- 
關系和boolean運算符
- ==, !=
- <, >, <=, >=
- &&, ||, !
- 三元運算符 xx ? xx : xx
-  - x < y ? x : y會返回較小的一個
 
- 位運算符: &, |, ^, ~, <<, >> P43
- 運算符優先級別P44
數學函數與常量
- 平方根: Math.sqrt(4) --> 2
- 冪運算: Math.pow(x, a) -->xa
- 三角函數: Math.sin, Math.cos, Math.tan, Math.atan, Math.atan2
- 指數函數,對數函數: Math.exp, Math.log, Math.log10
- Π和e常量: Math.PI, Math.E
- 靜態導入: import static java.lang.Math.*, 導入后就不用帶Math
強制類型轉換
-  截斷小數: 
-  - double x = 3.14159; int nx = (int)x;
 
-  舍入運算: 
-  - double x = 1.145; int nx = (int)Math.round(x);
-  得到最接近的整數, round返回long 
 
字符串
- 字串: s.substring(fromIndex, toIndex);
- 拼接: s1 + s2
- join: 把多個字符串放在一起, 用一個界定符分隔
-  - String.join("/", "S", "M", "L");
 
- repeat: String s = "hello".repeat(2)
-  - s = "hellohello";
 
- 不可變字符: 利用拼接改變字符串對象
- 字符串不是字符串數組, 不是char s[] = 'hello', 可以理解為 char* s = 'hello'
- 檢測字符串是否相等: s.equals(t)
-  - 忽略大小寫: "Hello".equalsIgnoreCase("hello")
 
- == 運算符只能確定兩個字符串是否在同一個位置上
- compareTo: 類似于C的strcmp函數, java用s.compareTo(t) == 0...
空串與null串
- 判空: s.length() == 0 或者 s.equals("")
- 判null: s == null
碼點與代碼單元
- length: 返回代碼單元數量
- codePointCount: 返回碼點數量
-  - int cpCount = s.codePointCount(0, s.length());
 
- P48…
構建字符串
- StringBuilder s = new StringBuilder(); s.append(ch); s.append(str);String completedString = s.toString();
輸入與輸出
讀取輸入
-  構造一個標準輸入流 
-  import java.util.*; 
-  - Scanner in = new Scanner(System,in); String name = in.nextline(); String firstName = in.next(); int age = in.nextInt();
 
-  密碼: 使輸入不可見 
-  -  Console cons = System.console();
String username = cons.readLine("User name: ");
char[] password = cons.readPassword("Password: ");
 
 
 
-  Console cons = System.console();
String username = cons.readLine("User name: ");
char[] password = cons.readPassword("Password: ");
 
格式化輸出
-  和C語言類似 
-  - System.out.printf("%8.2f", 333.333333);
 
-  用于printf的轉換符: d, x, o, g, e, f, s, c 
-  增加分隔符 
-  - System.out.printf("%,.2f", 1000.0/3.0);
 
文件輸入與輸出
-  讀取文件 Scanner in = new Scanner(Path.of("myfile.txt"), StandardCharset.UTF_8);
-  寫入文件 PrintWriter out = new PrintWriter("myfile.txt", StandarsCharset.UTF_8);
-  找到啟動目錄位置 String dir = System.getProperty("user.dir");
-  避免用一個不存在的文件構造Scanner: public static void main(String[] args) throws IOException....
控制流程
條件語句
- if (condition) {statement1;statement2; } else {statement1;statement2; }
循環
- while (condition){statement1;.... }
- do{statement1;.... }while(condition);
確定循環
- for(int i = 0; i < 100; i++)System.out.print(i )
多重選擇
- switch(choice) {case 1:...break;case 2:...break;...default:...break; }
中斷控制流程的語句
-  不帶標簽的break: while(i < 100) {...break; }
-  帶標簽的break語句, 用于跳出多重嵌套的循環語句: ... read_data; while(i < 1000){for(...){...break read_data;} }
-  continue語句, 將控制轉移到最內層循環的首部 while(x < y) {System.out.print(x);x = in.nextInt();if (x < 0)continue;... }
大數
-  import java.math.*; 
-  BigInteger , BigDecimal實現任意精度的整數和浮點數運算 
-  將普通樹轉換為大數: BigInteger a = BigInteger.valueOf(100);
-  構造一個大數: 
- 大數的運算:
數組
聲明數組
- int[] a , int a[]
- int[] a = new int[100];
- var a = new int[100];
- int[] smallPrimes = {2, 3, 5, 7, 11, 13} 不需要new, 也不需要指定長度
- new int[0];
訪問數組元素
-  數組長度: a.length 
-  初始化 var a = new int[100]; for(int i = 0; i < 100; i++)a[i] = i + 1;
for each循環
-  格式: 
-  for(variable: collection) statement 
-  這里的collection, 必須是實現了Iterable 接口的內對象或者是數組 
- for(int elem: a)System.out.print(elem)
-  打印數組中的所有值 
System.out.println(Arrays.toString(a));
數組拷貝
-  允許將一個數組變量拷貝到另一個數組變量, 這時兩個變量將引用同一個數組 
- int[] a = b; b[10] = 2; // now a[10] == 2
-  全部拷貝的方法: 
命令行參數
- 命令行調用程序Message.java
- 這里的args[0] == "-g"; args[1] = "cruel"; ...
- 程序名沒有存在args數組中
數組排序
- Arrays.sort(a);
- Arrays.binarySearch(a, startIndex, endIndex, value);
多維數組
-  double[][] a; 
- int[][] a = {{1, 2, 3},{4, 5, 60}, }
-  for each循環: 
-  - for(double[] row: a)for(double[] col: row)...
 
-  打印輸出二維數組: 
System.out.println(Arrays.deepToString(a));
不規則數組
- int[][] a = new int[N][]; for(int i = 0; i < N; i++)a[i] = new int[i+1];
總結
以上是生活随笔為你收集整理的CoreJava 笔记总结-第三章 Java的基本程序设计结构的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 笔记本升级攻略鸿基电脑如何升级
- 下一篇: 电脑怎么实现定时自动开关机定时自动开关机
