1. byte[] bytes = new byte[max];   //max must be a int, can not be long, so it's maximum Integer.MAX_VALUE.
  2. put the file.close() in finally part, instead of try part, so if IOException occured, the file can still be closed.
-- Sam 06:39 29/08/2014

数据结构
  1. java为数据结构提供了两个接口:Collection and Map.
  2. Collection has two sub interface: List and Set, 
  3. List are two different implementation; ArrayList and LinkedList, ArrayList actually contains an 数组,并通过System.arrayCoppy 来实现插入删除等动作。 而LinkedList通过新实现的数据结构,每个数据分别保持前面和后面的两个数据,从而实现快速的插入和删除。
  4. Set means spacial collection that has no duplicated elements, so it actually implemented with the help of Map's key.e.g. hasSet actually contains a hashMap that everykey has a same value. and hashMap keep an object[] in side to implement the operations.
  5. 二叉树定义:
    1. 若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
    2. 任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
    3. 任意节点的左、右子树也分别为二叉查找树。
    4. 没有键值相等的节点(no duplicate nodes)

-- Sam 10:49 12/09/2014
 Comparable interface and Comparator interface:
  1. Comparable is for object in list or array to implement, Comparator  is for a user defined Comparator to implement.
  2. So Comparable 's interface method is compareTo(Comparable  p), while the Comparator 's interface method is Compare(Comparable p1, comparable p2).
  3. is the item in List or Array already implemented Comparable interface, we can user List.sort() to do the sorting, while if the element didn't, or need a other than default way to sort, then we call Collects.sort( list, comparator) method to do the sort.
  4. String and all numbers has imeplemented Comparable interface. so we can call the Collections.sort(p) method directly. and ComparableTimSort will be used as default cmoparator, 
  5.  For other Lists which includs noncomparable object, you'd better use Collections.sort(p, cmparator) to give it a comparator, otherwise,  jdk will use  ComparableTimSort  用二分法对其排序。具体为:从头到尾便利,每次取后面和当前两个数,如果后面不比前面的大,那么用二分法寻找后面这个数比第几个大,并将其插入正确地方。这个默认比较是将对象直接做binary比较。(要注意,比较时那两个参数,第一个是后面的元素,第二个是前面的元素。
-- Sam 13:25 30/08/2014
 类型后面三个点(String...)
  • 从Java 5开始,Java语言对方法参数支持一种新写法,叫可变长度参数列表,其语法就是类型后跟...,表示此处接受的参数为0到多个Object类型的对象,或者是一个Object[]。 例如我们有一个方法叫做test(String...strings),那么你还可以写方法test(), test(String s),但你不能写test(String[] strings),这样会出编译错误,系统提示出现重复的方法。 
  • 在使用的时候,对于test(String...strings),你可以直接用test()去调用,标示没有参数,也可以用去test("aaa") 去掉,表示参数为{“aaa"},也可以用test(new String[]{"aaa","bbb"})。 
  • 另外如果既有test(String...strings)函数,又有test()函数,我们在调用test()时,会优先使用test()函数。只有当没有test()函数式,我们调用test(),程序才会走test(String...strings)。 
  • test(String s) 同理。
-- Sam 07:12 29/08/2014
 接口和抽象类
  1. 都不能被实例化,
  2. 抽象类里面不一定要有抽象方法, 
  3. 里面可以有变量声明,但接口里面的是常量。
-- Sam 17:00 24/08/2014
the diff between protected and default
  1. protected  和 default 一样,本包内可以访问。
  2. protected 还支持子类的夸包访问。
  3. 有点歧义的是,命名叫做protected,但是对于非子类也是开放的,只不过是要求本包内可见而已。
对于内部类
  1. public class A{ public ClassB{} } Class C()
  2. 内部类被实例化时,外部类必然已经被实例化了,因为如果从外部类或者静态方法中实力化内部类必须用a.new B()的形式实例化。
  3. 内类和外类互相可以访问私有变量,和调用私有方法.(不过如果内部类是个静态类,那么就不能调用外部类的变量了)。
  4. 内部类可以是静态类,外部类不能是静态类,外部类可以有静态方法,内部类不能有静态方法。
  5. 内部类中,可以通过A.this得到外部类的实例,当内部类和外部类有同名变量或方法时,这个很有用。
  6. 外部类用英语说是enclosing class, 或者Outer class, 内部类是enclosed class 或者Inner class.

方法重写(overwrite)
  1. overwrite时不光可以修改实现,还可以修改方法的可见性,比如可以把private方法覆盖时改为protected或者public。但注意只能改松,不能改严。
  2. 子类和父类的方法如果方法名和参数相同,即被认为是同一个方法,所以类(包括其子类中)不能再有同名同参数的方法,子类中有的话,视为重写(覆盖,overwrite),可以改变(只能放宽)可见性,不可以修改返回参数,是否throws异常的状态也不能修改。

-- Sam 17:22 24/08/2014
about String
  1. if we define 2 strings, like String a = "abc", String b = "abc", then a == b (they are same instance in permenent space.
  2. if you put a string into hashtable, or hash map, or arrayList, they are still the same instance.  they will not createa new instance. hastable.put("key", a); hash.get("key") == b;
  3. String is defined in perm heap to gether with the Classes, methods and other meta datas, so too many string may cause outOfMemoryError if do not increate the perm size.
-- Sam 12:19 22/08/2014
 启动参数
java -Dabc=def  -D2=2 org.cas.game.XXXX fff uuu ccc
  1. 启动程序后,System.getProperty("abc") != "def";   "def".equals(System.getProperty("abc")) 
  2. 所传递的系统参数和程序参数都是字符串形式。
-- Sam 08:15 22/08/2014
 Math methods
  1. Math.ramdom()返回的是大于等于0,但小于1的double。
  2. Math.ceil() 返回往大里数第一个整数,如果本身就是整数,那就是本身,但是以double形式表示。算table分页后占的页数时很管用。
  3. Math.floor()返回往小里数第一个整数,...................................................................................................。不知什么时候有用。
  4. Math.pow(2,3)==8.0 :2的3次方。
  5. Math.sqrt(4) == 2.0 :开方

-- Sam 16:22 17/08/2014
 多线程
  1. 线程执行顺序不可预测。
    本来以为主线程中new一个线程,调用其start()后,立马打印一个输出。那么start()后的输出必然先于run方法体内的语句执行,因为以前上百次调试都是发现立马跳到了start下方的语句的,而run方法体中的断点总是F8后才会停。
    今天测试后发现不是这样,如果不调试而是直接执行的话,从输出语句可以清楚看到,start()后的语句未必先被执行,而且并不是先start的线程先別调run。
  2. SwingUtility.invokeLater(new Runnable())不会产生一个新的线程,run方法仍然将在awt-event queue线程中运行。要使代码在新的线程中跑,必须使用new Thread(new Runnable()).
  3. 如何让一个线程在另一个线程后运行?可以一个线程作为另外一个线程的参数传入,在一个线程的run方法体内最后一行,调另一个线程的sart或者run方法。
  4. 其他的线程控制方法:
    1. 在一个线程中调用另一个的线程的join方法(另一个线程必须是已经启动了的,没有启动可以现启动,但必须启动,否则无效也无异常),cpu将在该行对该线程挂起,待那个线程执行完后再继续本线程。
    2. sleep()不管cpu是不是闲的,都不要调我。而且不释放锁,抱着锁睡觉。wait释放锁。
    3. yield给cpu一个重新选择调用的机会,我喜欢这个,否则,我哪知道该sleep多少秒?但注意cpu选择时多数优先选择级别高的线程,但不保证。
  5. 守护线程(也叫后台线程或者daemon thread)为其他线程服务用的,比如统计下活动线程数目啊啥的,当jvm发现前台线程都执行完了后,就果断退出,不会去管有没有后台线程。setDaemon(true)必须在其被start之前调。daemon thread创建的线程仍然是守护线程。
  6. The status of thread: newed(after newed), runnable(after started), running(after really picked by cpu), dead(after the run method finished). when thread is running, can make it into blocck status or sleep status, then it will be waked up and changed to runnablee status, but when a thread is dead, we can not call it's method to change it's status. 
  7. 在4核CPU上跑单线程程序,cpu不会到100%, 我在程序中同时new 4个线程,都start后,只见cpu里面飙至100%,再也不下来了。
  8. 在JUnit 的test方法中New Thread (new runnable(public void run())).start(). 发现run方法不会被执行,为什么?
  9.  线程安全
    1. 1) Immutable objects are by default thread-safe because there state can not be modified once created. Since String is immutable in Java, its inherently thread-safe.
      2) Read only or final variables in Java are also thread-safe in Java.
      3) Locking is one way of achieving thread-safety in Java.
      4Static variables if not synchronized properly becomes major cause of thread-safety issues.
    2. 5) Example of thread-safe class in Java: Vector, Hashtable, ConcurrentHashMap, String etc.
      6) Atomic operations in Java are thread-safe e.g. reading a 32 bit int from memory because its an atomic operation it can't interleave with other thread.
      7local variables are also thread-safe because each thread has there own copy and using local variables is good way to writing thread-safe code in Java.
      8) In order to avoid thread-safety issue minimize sharing of objects between multiple thread.
      9) Volatile keyword in Java can also be used to instruct thread not to cache variables and read from main memory and can also instruct JVM not to reorder or optimize code from threading perspective.
      为啥说Vector和hashtable 是线程安全的?因为里面的方法都用了Sychronized修饰符而已,然后,对于符合的动作,他们仍然不是线程安全的,所以干脆不要用他们。
  10. 用jvisualVM 进行thread dump,并监视简称状态。而且可以监视the jvm on remote linux machine.
-- Sam 16:11 21/09/2014
 java 定义常量不用const
鬼知道我怎么会记得有个const修饰符。
-- Sam 17:50 12/08/2014
文件读写
  1. 当new一个File时,如果传入的地址那里本来没有文件存在,那么这时也不会有文件被创建。而且若调用delete(),rename等方法什么也不会发生,也木有异常抛出。只有当那里本来有一个文件或者我们调.create()方法产生了文件后,这些方法才有有实际后果。
  2. java本来不支持文件读写,后来添加了reader类和writter类,都是抽象类,其中的read和write都是抽象方法。
  3. reader 和writer  是用来读写字符流的。而inputSteam和outputStream是用来读写字节流的。所以Reader和Writer的具体类的构造器需要stream的实例作参数。如。
  4. Reader <---InputStreamReader <---FileReader  Reader <--- BufferedReader 三个主要的用于文件读取的类,第一个是用于转码(若不设置则用系统默认编码读取)和读取read(char[] char, int offset, int length),,第二个提供了以文件名作为参数的构造器,没有增加任何新的方法: 第三个提供了readLine|方法。
  5. InputStream是抽象类 InputStream <--- FileInputStream.
  6. 务必记得flash和关闭文件流。
  7. RandomAccessFile的4种模式:
    1. r      只读 (如果文件不存在则抛异常,rw的话,如果文件不存在则自动建一个)
    2. rw   同时读和写,在File.close时文件会被存入硬盘。至于修改的同时会不会被存盘,取决于系统的实现。
    3. rws 每次write 或者修改文件信息时,都存盘。
    4. rwd 每次write都存盘,修改文件信息不存盘。
-- Sam 06:33 25/08/2014
what is immutable Class?
  1. 指属性不会发生改变的类,这种类线程安全,可以在线程间共享(不同担心有可能被别的线程修改了属性)。这种类不应该具有set方法,但可以有方法,比如String有replace方法,有subStr方法,但String是货真价实的immutable类,为啥?因为当调用这些方法的时候没有改变对象的状态,而是生成了一个新的String。
  2. BTW, String 要用new,如:String a_bad = new String(“stgo"),这样不好,因为这样100%创建了一个新对象,应该用 String a = "stgo".因为这样的, d话”stgo“是个可以共享的对象,所以如果以前有个String b = "stgo", 或者以后写个String b = "stgo", 就不会新建一个对像。a == b;          a_bad != b;      a_bad.equals(b)

-- Sam 17:46 12/08/2014

Please click here to login and add comments! || automatically refresh content every seconds