券多多是谁做的网站,软件工程学科评估排名,珠宝网站开发目的,用wordpress赚钱CoreJava 笔记总结 文章目录CoreJava 笔记总结第三章 Java的基本程序设计结构数据类型1. 整型2. 浮点类型3. char类型4. boolean类型变量与常量1. 变量2. 常量3. 枚举类型运算符关系和boolean运算符数学函数与常量强制类型转换字符串空串与null串码点与代码单元构建字符串输入与…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 最大32767longbyte -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.0var: 对于局部变量, 该关键字可以代替其类型 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 s2join: 把多个字符串放在一起, 用一个界定符分隔 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[i1];