java学习&复习,本文主要参照《Java核心技术卷》作为学习对象。
第一章 Java概述
java白皮书
简单性 面向对象 分布式 健壮性 安全性 体系结构中立 可移植性 解释型 高性能 多线程 动态性
- Java applet
在网页中运行的Java程序:applet
- JavaScript与Java
除去名字二者并无关系。Java是强类型的,比JavaScript捕捉错误的能力更强
第二章 Java环境
- Java专业术语
| 术语名 | 缩写 | 解释 |
|---|---|---|
| Java Development Kit(Java开发工具包) | JDK | 编写Java程序的程序员使用的软件 |
| Java Runtime Environment(Java 运行时环境) | JRE | 运行Java程序的用户使用的软件 |
| Server JRE(服务器 JRE) | 在服务器上运行 Java 程序的软件 | |
| Standard Edition(标准版) | SE | 用于桌面或简单服务器应用的 Java平台 |
| Enterprise Edition(企业版) | EE | 用于复杂服务器应用的 Java平台 |
| Micro Edition微型版) | ME | 用于小型设备的 Java平台 |
| Java FX | 用于图形化用户界面的一个备选工具包,在Java 11之前的某些Java SE 发布版本中提供 | |
| OpenJDK | Java SE 的一个免费开源实现 | |
| Java2 | J2 | 一个过时的术语,用于描述 1998~2006年之间的Java版本 |
| Software Development Kit(软件开发工具包) | SDK | 一个过时的术语,用于描述 1998~2006年之间的JDK |
| Update | u | Oracle 公司的术语,表示 Java8之前的 bug修正版本 |
| NetBeans | Oracle 公司的集成开发环境 |
- Java安装&编译
# 配置环境变量javac --version # 查看java版本
# 命令行编译javac welcome.javajava welcome- 集成开发环境
Eclipse | IntelliJ IDEA | NetBeans
- JShell
java即时运行
第三章 Java基本程序
- 驼峰命名
myClass
- 注释
//注释/*注释*//** *自动生成文档 *注释 */1 数据类型
- 整型
| int | 4字节 | -2^32 ~ 2^32 - 1 |
|---|---|---|
| short | 2字节 | -2^16 ~ 2^16 - 1 |
| long | 8字节 | -2^64 ~ 2^64 - 1 |
| byte | 1字节 | -2^8 ~ 2^8 - 1 |
1L/1l 长整型
0x 十六进制 0 八进制 0b/0B 二进制
- 浮点型
| float | 4字节 | 大约±3.40282347E+38F 6~7位 |
|---|---|---|
| double | 8字节 | 大约±1.79769313486231570E+308 15位 |
float 1f/1F
double 1D/1d
NaN <== 0/0 || sqrt(-n)

- char类型
\\b 退格 \\t 制表 \\n 换行 \\r 回车 \\" 双引号 \\' 单引号 \\\\ 反斜杠- unicode
16位:初期
码点 U+十六位
17代码平面 1(基本多语言平面U+0000 ~ U+FFFF 经典Unicode代码)
2~17(包括辅助字符 U+10000 ~ U+10FFF)- boolean类型
布尔类型 false || true 逻辑判断
2 变量
- 初始化
定义后需显式初始化,然后才能使用
final 常量 enum 枚举类型
3 运算符
+ - * / %
- Math
sqrt pow floorMod
三角函数 sin cos tan atan atan2
对数 exp log log10
pi与e近似值 Math.PI Math.E
可以import static java.lang.Math.*直接使用
- 类型转换
低精度 -> 高精度 无损失
高精度 -> 低精度 有损失-强制类型转换 (int) | …
+= *= %= ...k++ ++k== != > < >= <=&& ||x?a:b //(true:false)& | ^ ~ >> <<运算符优先级

4 字符串
"...".substring(l,r) //[l,r)"a"+"b""a".repeat(3) //ans = "aaa"String是不可变字符串
equals与==
==判断字符串是否处于同一位置,只能判断可以共享的字面量,对于不可共享的+||substring会出现错误
使用a.equals(b)或a.compareTo(b)用于字符串之间进行比较
- 空串和null串
"" null
- 码点与代码单元
int index = a.offsetByCodePoints(0,i); //第i个码点的位置int cp = a.codePointAt(index); //获取第i个码点//UTF-16部分字符需要两个代码单元,不能用charAt(pos)来获取- 字符串构建
StringBuilder builder = new StringBuilder(); //字符串构建器builder.append('a');builder.append(b);String res = builder.toString(); //生成字符串5 输入输出
- 输入
Scanner in = new Scanner(System.in);in.nextLine(); //读取一行in.next(); //以空格为分隔符读取in.nextInt(); //获取int型in.nextDouble(); //获取Double型in.hasNext(); //判断输入中是否还有其他内容- 输出
System.out.println();System.out.printf(“%8.2f",x); //类似于c中的printf// d 十进制 x 十六进制 o 八进制 f 定点浮点数 e 指数浮点数// s 字符串 c 字符 b 布尔 h 散列码- 文件输入输出
Scanner in = new Scanner(Path.of(""),StandardCharsets.UTF_8);PrintWriter out = new PrintWriter("",StandardCharsets.UTF_8);6 流程控制
// 块作用域{}
// 条件语句if(){
}else if(){
}else {}
// 循环while(...){} //先判断后执行do ... while(...); //先执行后判断
for(int i = ..;i <= .. ; i++) {}
switch (...){ case ...://标签可以是char byte short int 枚举常量 字符串字面量 ... break; ... default: ... break;//直到遇到break才会停止}
//break continue goto//break tag; 类似于goto 跳到tag:处- 大数
BigInteger BigDecimal
使用valueOf(x)转换x
7 数组
//数组定义int[] a = new int[len];int[] b = {1,2,3,4};new Type[0] || new Type[] {} //长度为0的数组,不同于null
//for each循环for(int i:a) ... //处理数组或其他元素集合
//- 数组拷贝a = b;a = Array.copyOf(b,copyLen);
//数组排序Arrays.sort(a); //快排 Math.random() -> [0,1)
//多维数组int[][] a = new int[lenA][lenB];a = { {...}, {...}};for(int[] i:a) for(int j:i) ...
//不规则数组int[][] a = new int[N][]; //包含N个指针的数组a = {...};
Java study and review. This article mainly references Java Core Technologies Volume as the object of study.
Chapter 1: Java Overview
Java White Paper
Simplicity, Object-oriented, Distributed, Robust, Secure, Architecture-neutral, Portable, Interpreted, High performance, Multithreading, Dynamism
- Java applet
A Java program that runs in a web page: applet
- JavaScript and Java
Apart from the name, the two are not related. Java is strongly typed and catches errors more effectively than JavaScript
Chapter 2: Java Environment
- Java Terminology
| Term | Abbreviation | Explanation |
|---|---|---|
| Java Development Kit (JDK) | JDK | Software used by programmers to write Java programs |
| Java Runtime Environment (JRE) | JRE | Software used by users to run Java programs |
| Server JRE (Server Runtime) | Software for running Java programs on a server | |
| Standard Edition (SE) | SE | Java platform for desktop or simple server applications |
| Enterprise Edition (EE) | EE | Java platform for complex server applications |
| Micro Edition (ME) | ME | Java platform for small devices |
| Java FX | An alternative toolkit for graphical user interfaces, provided in some Java SE releases prior to Java 11 | |
| OpenJDK | A free open-source implementation of Java SE | |
| Java2 | J2 | An obsolete term used to describe Java versions between 1998 and 2006 |
| Software Development Kit (SDK) | SDK | An obsolete term used to describe the JDK between 1998 and 2006 |
| Update | u | Oracle’s term, indicating bug-fix releases prior to Java 8 |
| NetBeans | Oracle’s integrated development environment |
- Java Installation & Compilation
# 配置环境变量javac --version # 查看java版本
# 命令行编译javac welcome.javajava welcome- Integrated Development Environments
Eclipse | IntelliJ IDEA | NetBeans
- JShell
JShell: Java’s interactive shell
Chapter 3: Java Basic Programs
- CamelCase Naming
myClass
- Comments
//注释/*注释*//** *自动生成文档 *注释 */1 Data Types
- Integers
| int | 4 bytes | -2^32 ~ 2^32 - 1 |
|---|---|---|
| short | 2 bytes | -2^16 ~ 2^16 - 1 |
| long | 8 bytes | -2^64 ~ 2^64 - 1 |
| byte | 1 byte | -2^8 ~ 2^8 - 1 |
1L/1l Long integer
0x Hexadecimal 0 Octal 0b/0B Binary
- Floating-point
| float | 4 bytes | Approximately ±3.40282347E+38F 6~7 digits |
|---|---|---|
| double | 8 bytes | Approximately ±1.79769313486231570E+308 15 digits |
float 1f/1F
double 1D/1d
NaN <= 0/0 || sqrt(-n)

- char type
\\b 退格 \\t 制表 \\n 换行 \\r 回车 \\" 双引号 \\' 单引号 \\\\ 反斜杠- Unicode
16-bit: early era
Code point U+ 16-bit
Planes 2–17 (including supplementary characters U+10000 ~ U+10FFF)
- boolean type
Boolean type false || true Logical evaluation
2 Variables
- Initialization
After defining, you must explicitly initialize before you can use it
final constants enum types
3 Operators
+ - * / %
- Math
sqrt pow floorMod
Trigonometric functions sin cos tan atan atan2
Logarithms exp log log10
Pi and e approximations Math.PI Math.E
You can import static java.lang.Math.* to use them directly
- Type conversion
Low precision -> high precision is lossless
High precision -> low precision incurs loss - explicit casting (int) | …
+= *= %= ...k++ ++k== != > < >= <=&& ||x?a:b //(true:false)& | ^ ~ >> <<Operator precedence

4 Strings
"...".substring(l,r) //[l,r)"a"+"b""a".repeat(3) //ans = "aaa"Strings are immutable
equalsand==
== checks whether strings are in the same location, and can only reliably compare shared literals; for non-shared concatenations or substring there can be errors
Use a.equals(b) or a.compareTo(b) for comparing strings
- Empty strings and null strings
"" null
- Code points and code units
int index = a.offsetByCodePoints(0,i); //position of ith code pointint cp = a.codePointAt(index); //get the ith code point//UTF-16 surrogate pairs require two code units; cannot use charAt(pos)- Building strings
StringBuilder builder = new StringBuilder(); //String builderbuilder.append('a');builder.append(b);String res = builder.toString(); //generate string5 Input/Output
- Input
Scanner in = new Scanner(System.in);in.nextLine(); //read a linein.next(); //read with space delimiterin.nextInt(); //read intin.nextDouble(); //read Doublein.hasNext(); //check if there is more input- Output
System.out.println();System.out.printf(“%8.2f",x); //similar to printf in C// d decimal x hexadecimal o octal f fixed-point e exponential// s string c character b boolean h hash code- File Input/Output
Scanner in = new Scanner(Path.of(""),StandardCharsets.UTF_8);PrintWriter out = new PrintWriter("",StandardCharsets.UTF_8);6 Control Flow
// 块作用域{}
// 条件语句if(){
}else if(){
}else {}
// 循环while(...){} // Evaluate condition first, then executedo ... while(...); // Execute first, then evaluate
for(int i = ..;i <= .. ; i++) {}
switch (...){ case ...://Label can be char byte short int enum constants string literals ... break; ... default: ... break;//Will stop only when break is encountered}
//break continue goto//break tag; Similar to goto jumping to tag:- Large numbers
BigInteger BigDecimal
Use valueOf(x) to convert x
7 Arrays
//数组定义int[] a = new int[len];int[] b = {1,2,3,4};new Type[0] || new Type[] {} //Length-0 arrays, different from null
//for each loopfor(int i:a) ... //process array or other element collections
//- Array copyinga = b;a = Array.copyOf(b,copyLen);
//Array sortingArrays.sort(a); //Quicksort Math.random() -> [0,1)
//Multidimensional arraysint[][] a = new int[lenA][lenB];a = { {...}, {...}};for(int[] i:a) for(int j:i) ...
//Jagged arraysint[][] a = new int[N][]; //Array of N referencesa = {...};
Javaの学習と復習として、本稿は主に《Java核心技术卷》を参照して学習対象とします。
第1章 Javaの概要
Javaホワイトペーパー
簡潔性 オブジェクト指向 分散性 堅牢性 安全性 アーキテクチャ中立性 移植性 解釈型 高性能 マルチスレッド 動的性
- Javaアプレット
ウェブページ上で実行されるJavaプログラム: applet
- JavaScriptとJava
名前が同じでも両者には関係がありません。Javaは静的型付けで、JavaScriptよりもエラーを検出する能力が強いです
第2章 Java環境
- Java専門用語
| 用語名 | 略語 | 説明 |
|---|---|---|
| Java Development Kit(Java開発キット) | JDK | Javaプログラムを作成するプログラマーが使用するソフトウェア |
| Java Runtime Environment(Java実行環境) | JRE | Javaプログラムを実行するユーザーが使用するソフトウェア |
| Server JRE(サーバーJRE) | サーバー上でJavaプログラムを実行するソフトウェア | |
| Standard Edition(標準エディション) | SE | デスクトップまたは簡易サーバーアプリケーション向けのJavaプラットフォーム |
| Enterprise Edition(エンタープライズ版) | EE | 複雑なサーバーアプリケーション向けのJavaプラットフォーム |
| Micro Edition(マイクロエディション) | ME | 小型デバイス向けのJavaプラットフォーム |
| JavaFX | グラフィカルユーザーインターフェースの代替ツールキットで、Java 11以前の一部のJava SEリリースで提供されていました | |
| OpenJDK | Java SE の無料のオープンソース実装 | |
| Java2 | J2 | 1998~2006年のJavaバージョンを説明するための時代遅れの用語 |
| Software Development Kit(ソフトウェア開発キット) | SDK | 1998~2006年のJDKを指す時代遅れの用語 |
| Update | u | Oracle社の用語で、Java8以前のバグ修正リリースを示す |
| NetBeans | Oracle社の統合開発環境 |
- Javaのインストールとコンパイル
# 配置環境変数javac --version # 見るJavaのバージョン
# コマンドラインでのコンパイルjavac welcome.javajava welcome- 統合開発環境
Eclipse | IntelliJ IDEA | NetBeans
- JShell
Javaの対話的実行
第3章 Java基本プログラム
- キャメルケース命名
myClass
- コメント
//注释/*注释*//** *自動生成ドキュメント *注释 */1 データ型
- 整数型
| int | 4バイト | -2^32 ~ 2^32 - 1 |
|---|---|---|
| short | 2バイト | -2^16 ~ 2^16 - 1 |
| long | 8バイト | -2^64 ~ 2^64 - 1 |
| byte | 1バイト | -2^8 ~ 2^8 - 1 |
1L/1l 長整数型
0x 十六進法 0 は十進法 0b/0B 二進法
- 浮動小数点型
| float | 4バイト | 約±3.40282347E+38F 6~7桁 |
|---|---|---|
| double | 8バイト | 約±1.79769313486231570E+308 15桁 |
float 1f/1F
double 1D/1d
NaN <== 0/0 || sqrt(-n)

- char型
\\b 退格 \\t タブ \\n 改行 \\r 復帰 \\" ダブルクォート \\' シングルクォート \\\\ バックスラッシュ- Unicode
16ビット:初期
コードポイント U+十六ビット
17コード平面 1(基本多言語平面U+0000 ~ U+FFFF 伝統的なUnicodeコード)
2~17(補助字符 U+10000 ~ U+10FFF)- boolean型
Boolean型 false || true 論理判定
2 変数
- 初期化
定義後には明示的な初期化が必要で、初期化されていないと使用できません
final 常量 enum 列挙型
3 演算子
+ - * / %
- Math
sqrt pow floorMod
三角関数 sin cos tan atan atan2
対数 exp log log10
πとeの近似値 Math.PI Math.E
import static java.lang.Math.*を使用して直接利用可能
- 型変換
低精度 -> 高精度はロスなし
高精度 -> 低精度 有損失-強制型変換 (int) | …
+= *= %= ...k++ ++k== != > < >= <=&& ||x?a:b //(true:false)& | ^ ~ >> <<演算子の優先順位

4 文字列
"...".substring(l,r) //[l,r)"a"+"b""a".repeat(3) //ans = "aaa"Stringは不変の文字列
equalsと==
==は文字列が同じ参照を指しているかどうかを判断します。リテラルで共有される場合にのみ有効で、共有されない+やsubstringでは誤りが生じます
文字列間の比較にはa.equals(b)またはa.compareTo(b)を用います
- 空文字列とnull文字列
"" null
- コードポイントとコードユニット
int index = a.offsetByCodePoints(0,i); //第iコードポイントの位置int cp = a.codePointAt(index); //第iコードポイントを取得// UTF-16の部分的な文字は2つのコードユニットを必要とするため、charAt(pos)では取得できない- 文字列構築
StringBuilder builder = new StringBuilder(); //文字列ビルダーbuilder.append('a');builder.append(b);String res = builder.toString(); //文字列を生成5 入力出力
- 入力
Scanner in = new Scanner(System.in);in.nextLine(); //1行を読むin.next(); //空白で区切って読み取るin.nextInt(); //int型を取得in.nextDouble(); //Double型を取得in.hasNext(); //入力に他の内容があるか判定- 出力
System.out.println();System.out.printf(“%8.2f",x); //C言語のprintfに似ています// d 十進数 x 十六進数 o 八進数 f 固定小数点数 e 指数表記// s 文字列 c 文字 b 真偽 h ハッシュコード- ファイル入出力
Scanner in = new Scanner(Path.of(""),StandardCharsets.UTF_8);PrintWriter out = new PrintWriter("",StandardCharsets.UTF_8);6 制御フロー
// ブロックのスコープ{}
// 条件文if(){
}else if(){
}else {}
// ループwhile(...){} // 先に判定してから実行do ... while(...); // 先に実行してから判定
for(int i = ..;i <= .. ; i++) {}
switch (...){ case ...://ラベルは char byte short int 枚挙定数 文字列リテラル ... break; ... default: ... break;//breakに出会うまで停止}
//break continue goto//break tag; Gotoのように tag: の場所へ飛ぶ- 大数
BigInteger BigDecimal
valueOf(x)を使用してxを変換
7 配列
//配列の定義int[] a = new int[len];int[] b = {1,2,3,4};new Type[0] || new Type[] {} //長さ0の配列、nullとは異なる
//for eachループfor(int i:a) ... //配列または他の要素集合を処理
//- 配列のコピーa = b;a = Array.copyOf(b,copyLen);
//配列のソートArrays.sort(a); //クイックソート Math.random() -> [0,1)
//多次元配列int[][] a = new int[lenA][lenB];a = { {...}, {...}};for(int[] i:a) for(int j:i) ...
//不規則配列int[][] a = new int[N][]; //N個の指针を含む配列a = {...};部分信息可能已经过时









