基础

基本和 $C++$ 一样

注释

1
2
3
4
5
// 单行注释

/*
多行注释
*/

数据类型

1
2
3
4
5
6
7
8
9
int;
long;
short;
float;
double;
byte;
boolean;

final // = C++ const

循环 & 条件

1
2
3
4
5
6
7
8
9
10
//和 C++ 一样
while;
do-while;
for;

break;
continue;

if-else;
switch;

函数 $(function)$

1
2
3
4
5
type function_name (parameters) {
//implement
}

//重载 同 C++

基础类

Math

1
2
3
4
5
6
7
8
9
10
11
12
13
14
abs(); // 绝对值
ceil(); // 向上取整
floor(); // 向下取整
round(); // 四舍五入
max();
min();
pow();
log();
sin();
cos();
sqrt();
atan();
atan2();
random();

Character

1
2
3
4
5
6
7
isLetter();
isDigit();
isUpperCase();
isLowerCase();
toUpperCase();
toLowerCase();
toString();

String

1
2
3
4
5
6
charAt(index); //return s[index]
concat(str); // append str to s
equals(); // == is incorrect!!!
length();
toUpperCase();
toLowerCase();

StringBuffer

1
2
3
4
append(str);
reverse();
delete(l, r); // delete s[l - r]
replace(l, r, str); //replace s[l - r] with str

Input

Scanner

1
2
3
4
5
6
7
8
9
10
11
Scanner input = new Scanner(System.in);
int data = input.nextInt();
int dataname = input.nextInt();
long dataname = input.nextLong();
short dataname = input.nextShort();
float dataname = input.nextFloat();
double dataname = input.nextDouble();
byte dataname = input.nextByte();
boolean dataname = input.nextBoolean();
String dataname = input.next();//read a string
String dataname = input.nextLine();//read a line

Output

1
2
System.out.println(dataname); // endl
System.out.print(dataname); // no endl

类 $(class)$

1
2
3
4
5
6
// 同 C++
private:
public:
protected:

//构造 同 C++

继承 $(Inheritance)$

1
2
3
4
5
6
extends classname// 表示继承 classname

// 子类不会自动调用父类构造函数!!!
super(parameters); //调用父类构造函数!!!

// java 不支持多继承 即一个类只能继承一个类,不能继承多个类

重写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//参数列表与被重写方法的参数列表必须完全相同
//构造方法不能被重写
//如果不能继承一个类,则不能重写该类的方法
class Animal {
void move() {

}
}

class Dog extends Animal {
void move() {//Overload
//implement
}
}

多态 $(Polymorphism)$

1
2
3
4
5
6
//继承
//重写
//父类引用子类对象
Parent p = new Child();

//同 C++

异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
try {

}catch(ExceptionName var_name1) {

}catch(ExceptionName var_name2) {

}
...

throws ExceptionName;//抛出异常

finally { //无论是否异常 finally 内的语句 总会被执行
//code
}

$I / O$

1
//to do

ArrayList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ArrayList;
// 操作
size(); //
sort(); // sort
add(x); // = append(x)
remove(index); // remove list[index]

get(index); // = list[index]
set(index, x); // set list[index] = x

// T
Integer;
Double;
Long;
Short;
String;
Boolean;

HashSet

1
2
3
4
5
6
7
8
add();//
remove(x);
contains(x);// 是否包含x

size();
clear();
clone();
isEmpty();

HashMap

1
2
3
4
5
6
7
8
9
10
11
12
13
size();
clear();
clone();
isEmpty();

get(key);
put(key, value);

remove(key);
replace(key, new_value);

containsKey(); true / false
containsValue(); true / false

后记

一天速通 $Java$ 考试 。