CoreJava 笔记总结
文章目录
- CoreJava 笔记总结
- 第三章 Java的基本程序设计结构
- 数据类型
- 1. 整型
- 2. 浮点类型
- 3. char类型
- 4. boolean类型
- 变量与常量
- 1. 变量
- 2. 常量
- 3. 枚举类型
- 运算符
- 关系和boolean运算符
- 数学函数与常量
- 强制类型转换
- 字符串
- 空串与null串
- 码点与代码单元
- 构建字符串
- 输入与输出
- 读取输入
- 格式化输出
- 文件输入与输出
- 控制流程
- 条件语句
- 循环
- 确定循环
- 多重选择
- 中断控制流程的语句
- 大数
- ==**数组**==
- 声明数组
- 访问数组元素
- ==for each循环==
- 数组拷贝
- 命令行参数
- 数组排序
- 多维数组
- 不规则数组
第三章 Java的基本程序设计结构
punlic访问修饰符- 类名: 以大写字母开头的名词, 骆驼命名法
- 源代码的文件名字必须与公共类名字相同
main方法必须声明为public- 每个
java应用程序都必须有一个main方法
public class ClassName
{public static void main(String[] args){program statements}
}
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_IFINITYDouble.NEGATIVE_IFINITYDouble.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: ");
-
格式化输出
-
和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); -
构造一个大数:
BigInteger big = new BigInteger("14862301485623021546230");
- 大数的运算:
+ BigInteger c = big.add(b); // c = big + b;
* BigInteger d = c.multiply(b.add(BigInteger.valueof(2))); //d = c * (b + 2);
/ BigInteger e = d.divide(d);
- substract , % mod
数组
声明数组
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 -
全部拷贝的方法:
int[] b = Arrays.copyOf(a, a.length);
多余元素置0或false, 第二个参数太小就拷贝前面的元素
命令行参数
- 命令行调用程序
Message.java
java Message -g cruel world
- 这里的
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];