传递机制
值传递:
copy后,改变一个变量的值,其它变量的值不会被改变。因为其拷贝了值。
所有numeric类 & String
引用传递:
copy后,改变一个变量的值,其它变量的值会随之改变。因为其指针指向原address。
List等Collection & 自己造的类。
public static void main(String[] args) {
String st1 = "AAA st1";
String st11 = st1;
System.out.println(st1.hashCode() + "\t" + st11.hashCode()); // -519811727 -519811727
System.out.print("Before change value, are they == ? \t--");
System.out.println(st1==st11); // true
st1 = st1.toLowerCase(Locale.ROOT);
System.out.println(st1+ "\t" +st11); // aaa st1 AAA st1
System.out.println(st1.hashCode() + "\t" + st11.hashCode()); // -1238779503 -519811727
System.out.print("After change value, are they == ? \t--");
System.out.println(st1==st11); // false
int in1 = 10;
int in11 = in1;
System.out.print("Before change value, are they == ? \t--");
System.out.println(in1==in11); // true
in1 += 1;
System.out.println(in1 + "\t" + in11); // 11 10
System.out.print("After change value, are they == ? \t--");
System.out.println(in1==in11); // false
String st2 = "AAA st1";
System.out.println("Initialize a same value String, see its hash code is: " + st2.hashCode()); // -519811727
}
Switch
- 没有进入任何case 则执行default
- 进入case但没有break 则无脑执行后面case&default 直到遇到break
List初始化
- 元素类型用原始数据类型(Double / Integer / ...),不能使用基本数据类型(double / int / ...)
- 元素可以是Object
- 元素可以是Exception
I/O 用到的类们&它们的param
Compare相关
1. 调用sort方法,自己定义compare方法
Collections.sort(persons, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getAge() - p2.getAge();
}
});
// or use lambda
Collections.sort(persons, (p1, p2) -> p1.getAge() - p2.getAge());
2. 实现Comparable接口,自己定义compareTo方法
class Person implements Comparable<Person> {
public int compareTo(Person person) {
int result = surname.compareTo(person.surname);
return result == 0 ? firstName.compareTo(((Person) person).firstName) : result;
}
}
3. 泛型元素实现了Comparable接口,即<T extends Comparable<T>>,就可以调用其compareTo方法
(可以用于:当Array的元素类型是泛型,需要sort此Array时)
// sort an Array with generic type element
<T extends Comparable<T>> void insertionSort(T arr[]) {
int i, j;
T key;
for (i = 1; i < arr.length; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j].compareTo(key) > 0) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
关于<T extends Comparable<T>>:
关于try-catch
finally一定会被执行!即使在try部分出现了return也阻止不了finally先被运行
一些个原则
抽象类实现接口
抽象类实现接口不需要实现接口里声明的方法。(当然它的非抽象子类们还是需要实现的)
java抽象类实现接口可以不用实现方法_creator123123的博客-CSDN博客java抽象类实现接口可以不用实现方法学习struts2时,拦截器接口Interceptor继承了Serializable接口,拦截器抽象类AbstractInterceptor实现了Interceptor接口,在AbstractInterceptor类中却没有声明或定义Serializable中的writeobject等其他方法https://www....https://blog.csdn.net/creator123123/article/details/86159588