1. 有关copy
Foo x = new Foo(1);
Foo y = new Foo(2);
x = y; // 只是指针copy
Foo copy = new Foo(x); // 对象copy, 堆内存, 不建议使
2. Array 相关
int[] a = new int[100];
a[0] = 13;
a[1] = 15;
a.length // 100
a[100] = 13; // 抛出异常
Array Class // array 已经不在是传统意义的数组, 封装了很多方法
1) array copy: System.arraycopy(source-array, source-index,
dest-array, dest-index, length-to-copy);
只能copy1维数组
2) array equals(): deep equals()
3) Multidimensional arrays:
int[][] grid = new int[100][100];
grid[0][1] = 10; // 引用数组元素
多维数组可以分别访问行列
int temp;
int[][] grid = new int[10][20];
grid[0][0] = 1;
temp = grid.length; // 10, 有10行
temp = grid[0].length; // 20, 有20列
int[] array = grid[0]; // 1维数组的copy, copy 了 1列
temp = array.length; // 20
4) 数组可以使用直接字面量定
3. Static
static varibale
A static variable is like a global variable, except it exists inside of a class.
There is a single copy of the static variable inside the class. In contrast, each instance variable
exists many times -- one copy inside each object of the class.
Static variables are rare compared to ordinary instance variables.
The full name of a static variable includes the name of its class. So a static variable named "count" in
the Student class would be referred to as "Student.count". Within the class, the static variable can be
referred to by its short name, such as "count", ut I prefer to write it the long way, "Student.count",
to emphasize to the reader that the variable is static.
e.g. "System.out" is a static variable "out" in the System class that represents standard output.
static method
A static method is like a regular C function that is defined inside a class.
A static method does not execute against a receiver object. Instead, it is like a plain C function -- it
can have parameters, but there is no receiver object.
Just like static variables, the full name of a static method includes the name of its class, so a static foo()
method in the Student class is called Student.foo().
In contrast, a regular method in the Student class would be invoked with a message send (aka a method
call) on a Student object receiver like s.getStress(); where s points to a Student object.
It is better to call a static method like this: Student.foo(), NOT s.foo(); where s points to a Student object,
although both syntaxes work.
4. system.arraycopy( source-array, source-index, dest-array, dest-index, length-to-copy) 如果数组元素很多, 这样做, 只能是一维数组
会比 for 循环更有效率. ( 只能做一维数组 )
5. a = b 只是比较数组指针, Arrays.equals(a, b) 是堆数组的真正比较
6. 多维数组, 可以看做是两个一维数组, 例如:
多维数组
8. “List” 相关, 在 java 中有两个list, 一个是 java.util.list 链表, 另一个是 java.awt.list 图形中的List, 如果两个import
你都指定了, 例如 import java.util.*; import java.awt.*; 那么在引用 List时系统就会出现歧义, 到底你想用哪个List,这时就
要指定全称, 例如 java.util.List.
9. jar files .jar file is a standard way of packaging a bunch of .class files all together.
10. import 很多时并不会减慢程序速度, 主要作用是让你在引用别的类时可以不指定全名.
11. java 命令行模式
javac a.java, 注意, 如果一个目录下有很多.java文件需要编译, 那么使用这种方式比较好, javac *.java, 快的很.
-classpath, javac –classpath /usr/class/cs108/jar/junit.jar:. 等, 就会将类编译进这个文件夹( 在jsp中有重复内容 ),用:冒号做分隔符
-claspath, 在运行时的classpath, 表明到这个地方去查找你要用到的类.
参数, 因为main 函数可以接收参数再运行static void main(String[] args), 所以可以传String 类型参数给main, 例如 java Foo aaa bbb
13.Files, java输入输出流.
14.Exception
An exception occurs at runtime when a line of code tries to do something impossible such as accessing an
array using an index number that is out of bounds of the array or dereferencing a pointer that is null.
An exception halts the normal progress of the code and searches for error handling code that matches the
exception. Most often, the error handling code will print some sort of warning message and then possibly
exit the program, although it could take some more sophisticated corrective action.
15. Diagnosing Exceptions
When your program crashes with an exception, if you are lucky you will see the exception stack trace output.
In production code, the catch will often exit the whole program, using a non-zero int exit code to indicate a
program fault (e.g. call System.exit(1)). Alternately, the program could try to take corrective action in the
catch-block to address the situation.Avoid leaving the catch empty, put a printStackTrace() in the catch so
you get an indication of what happened. If no exception occurs during the run, the catch-block is ignored.
When an exception is thrown at runtime, it looks for the first matching catch(...) clause so "catch
(Exception e)" would catch any type of exception, but "catch (IOException e)"would catch only IOExceptions.