目录
一、基础常识
(1)命名规则
- 类名:驼峰命名法,且首字母大写(例如:FirstSample)
- 源代码文件名与公共类的名字相同(例如:FirstSample. java)
- 方法名:驼峰命名,且首字母小写(例如:getName)
- 包名:小写字母
- 常量名:全大写字母,_分隔
(2)注释
- // 普通注释
- /* 和 */长篇注释,且不可嵌套
- /** 和 */ 自动生成文档
二、数据类型
- 浮点数值采用二进制系统标识,无法精确表示1/10,例如:
System.out.println(2.0-1.1)
结果为0.8999999999999999 - 建议不要使用char
三、变量与常量
- 逐一声明变量提高程序的可读性
- 必须显示初始化变量,不要使用未初始化变量
- final声明常量,static final声明类常量
- 枚举,可以把相关的常量分组到一个枚举类型里
代码举例:
public class EnumTest {
public static void main(String[] args) {
// 使用==比较
System.out.println(Direction.EAST==Direction.EAST);
// 将d设置为EAST
Direction d= Direction.valueOf(Direction.class,"EAST");
System.out.println(d.toString()); // EAST
// 包换全部枚举值得数组
Direction[] staff= Direction.values();
for(Direction i :staff)
System.out.println(i);
//枚举常量位置
System.out.println(Direction.EAST.ordinal());// 0
System.out.println(Direction.EAST.getS()); // 东
show(Direction.NORTH);
}
// 可运用switch语句
static void show(Direction direction) {
switch (direction) {
case EAST:
System.out.println(direction.getS());
break;
case WEST:
System.out.println(direction.getS());
break;
case SOUTH:
System.out.println(direction.getS());
break;
case NORTH:
System.out.println(direction.getS());
break;
}
}
}
enum Direction{
EAST("东"),
WEST("西"),
SOUTH("南"),
NORTH("北");
private String s;
// 构造器只允许是私有的
private Direction(String s ) {
this.s=s;
}
public String getS() {
return s;
}
}
四、运算符
- 15/2=7 而15.0/2=7.5
注:添加
import static java.long.Math
便可以无需声明前缀Math
- 根号
Math.sqrt(x)
- x的a次幂
Math.pow(x,a)
- 强制类型转换:
double x=9.98741
int y=1
(int)x
// 值为9 (截断小数点)
y+=3.5
<=>(int) y+3.5
Math.round (x)
//值为10 (”四舍五入”) - 极少数情况将布尔类型转换为整型 ,则使用条件表达式:b? 1:0
五、字符串
方法 | 说明 |
---|---|
substring(1,3) | 从字符串提取子串【1,3) |
join(“/”,“a”,“b”) | 用定界符连接元素 “a/b” |
equals() | 检测两个字符串是否相等 boolean |
length() | 字符串长度 int n |
replace(old,new) | 内容替换 用new替换所有old |
trim | 去除头尾空格 |
- 使用euqals计较字符串是否相等,而非==
- 检查字符串是否为空
if ( str.length ()==0 )
或
if ( str.equals(" "))
六、输入与输出
(1)输入
使用
import java.util.*
导入包
代码示例:
Scanner scanner=new Scanner(System.in);
System.out.println("请输入一个整数");
int a =scanner.nextInt();
System.out.println("请输入字符串");
String b =scanner.nextLine();
System.out.println("结果为"+a+b);
System.out.println("结束");
方法 | 说明 |
---|---|
nextInt() | 不接收空格,回车结束,不吸收回车 |
nextLine() | 接收空格,回车结束,吸收回车 |
next() | 不接收空格,回车结束,吸收回车 |
hasNext() | 检测是否还有其他单词 boolean |
如上代码中:若在输入整数后输入回车,则回车会直接被nextLine()捕获且结束,相当于b中没有捕获到任何内容;若输入空格,则空格会被nextLine()捕获且继续捕获输入内容,直到回车结束
(2)输出
print()
输出不换行println()
换行printf()
格式化输出(“%a.bf”,x)表示a个字符宽度,保留b位小数的浮点数
七、文件写入与读取
- 若文件名中包含反斜杠,则要在所有反斜杠前再加一个反斜杠:“C:\\asd\\dsa\\a.txt”
代码示例:
import java.io.*;
import java.util.*;
public class ReadAndWrite {
public static void main(String[] args) throws Exception {
File file=new File("C:\\Users\\Lin\\Desktop\\a.txt");
//文件写入
PrintWriter out = new PrintWriter(file,"utf-8");
for(int i =0;i<=10;i++){
out.println(i);
}
out.close();
//文件读取
Scanner in = new Scanner(file,"utf-8");
while(in.hasNext()) {
System.out.println(in.next());
}
in.close();
}
}
- 文件写入:
PrintWriter out = new PrintWriter(file,"utf-8")
- 文件读取:
Scanner in = new Scanner(file,"utf-8")
- 注意close()以及 抛出异常throws…
八、流程控制
(1) 条件语句
if else
(2) 循环语句
while
do while
for
(3) 多重选择
switch case default
代码示例:
switch (choice) {
case 1:
...
break;
case 2:
...
break:
......
default:
...
break;
}
- 从与之匹配的case标签开始执行,直到遇到break语句;若没有break,继续执行下一个case标签
- 没有匹配的case标签,则执行default
- case标签类型可以为char,byte,short,int,枚举,字符串
(4)中断控制
1、除了普通的break语句,还有一种带标签的break语句
代码示例:
XXX:
while(...) {
for(...) {
if(...)
break XXX;
}
}
标签放在想要跳出的最外层循环/条件之前,注意只能跳出,不能跳入
2、continue 跳出本次循环
九、大数值
在此不展开介绍。在特定场合应用,BigDecimal大数值与精确计算详细介绍
十、数组
(1)创建数组
代码示例:
int [] a = new int[10]; //所有元素初始化为0
boolean [] b= new boolean[10]; //所有元素初始化为false
String [] c = new String[10]; //所有元素初始化为null
一旦创建了数组就无法改变它的大小,若需要运行中扩展数组大小,应使用数组列表ArrayList (待补充)
允许长度为0,0与null不同
(2)遍历数组
代码示例:
int [] a = {1,2,5,3};
// 传统for循环
for(int i=0;i<a.length;i++) {
System.out.println(a[i]);
}
// for each循环
for(int j: a) {
System.out.println(j);
}
//快速打印
System.out.println(Arrays.tostring(a));
for each 循环会遍历数组的每个元素
(3)常用API
方法 | 说明 |
---|---|
tostring(a) | 返回a中元素字符串[ , ] |
copyOf(a,n) | 返回前n个a中元素 |
copyOfRange(a,m,n) | 返回 [m,n) a中元素 |
sort() | 数组排序 |
(4)多维数组
代码示例:
int [][]a={
{...},
{...}
};
//遍历多维数组
for(double[] i :a){
for(double j : i){
System.out.println(j);
}
}
//快速打印
System.out.println(Arrays.deepToString(a));