1.异常
Throwable-->Error,Exception
RuntimeExpeption:如除数为0,空指针
2.final(加上final就代表是最终的,不能被改变)
变量+final:一旦被初始化,不可改变。基本类型值不能变,对象变量引用不能变。
方法中的参数+final:基本类型:传值,对象变量:传递引用,防止意外改变
方法+final:可以继承,不允许继承中的覆写;转化为inline调用的机制(省去了保存断点,压栈)
类+final:不能被继承,final类中的方法自然是final型的,final类中的成员可以定义为final型,也可以不是final
finally:在finally中可以维护对象的内部状态,并可以清理非内存资源
finalize:用于一些不容易控制,并且非常重要的资源的释放
3.传值与传引用
程序:
public class Test2 {
public static void main(String []args){
{
// test0
System.out.println("test0");
int j=0;
for(int i=0;i<100;i++){
j=j++;
}
System.out.println(j);
}
{
// test1
System.out.println("test1");
StringBuffer str =new StringBuffer("Hello");
test1(str);
System.out.println(str);
}
{
// test2
System.out.println("test2");
String str2="Hello";
test2(str2);
System.out.println(str2);
}
}
private static void test2(String str2) {
str2="world";
}
private static void test1(StringBuffer str) {
str.append(" world");
}
}
答案:
test0
0
test1
Hello world
test2
Hello
public class Test3 {
public static void main(String[] args) {
char ch[]={'H','e','l','l','o'};
change(ch);
System.out.println(ch);
}
private static void change(char[] ch) {
ch[0]='h';
}
}
答案:
hello
class Value {
public int i = 15;
}
public class Test4 {
public static void main(String[] args) {
Test4 t = new Test4();
t.first();
}
private void first() {
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
private void second(Value v, int i) {
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + "\t" + i);
}
}
答案:
15 0
20
不懂为什么第二个是20?val属于局部变量,方法结束销毁了,v还能指向原来的对象????
4.静态变量与私有变量
程序示例:
public class Test5 {
static boolean Paddy;
public static void main(String[] args) {
System.out.println(Paddy);
}
}
答案:
false
Java默认布尔变量是false的5.输入/输出流
RandomAccessFile
new BufferedReader(new InputStreamReader(new FileInputStream(filename))) 很多字节数的文本文件
Java流类图结构
字符流:Reader/Writer
字节流:InputStream/OutputStream
6.序列化
Serializable接口
转换为byte
lightweight persistence
序列化,解序列化
目的:Java的远程方法调用 remote method invocation(RMI)
对于JavaBean,对象序列化也是不可少的(JavaBean?)
对象网
二.内存管理
1.GC:JVM用于释放那些不再使用的对象所占用的内存。
软指针:不直接指向对象,而是指向对象的引用。
Java使用一系列软指针来跟踪对象的各个引用,并用一个对象表将这些软指针映射为对象的引用。通过软指针,Java的垃圾收集器能够以单独的线程在后台运行,并依次检查每个对象,通过更改对象表项,垃圾收集器可以标记对象、移除对象、移动对象或检查对象。
System类的gc()方法