前一阵子把scjp的资料又翻阅了一遍,这里把容易弄错的要点摘出来备忘。(可能我以后相当一段长时间不会再碰java了)
11. hashCode
- If you don’t override equals(), your objects won’t be useful hashtable/ hashmap keys.
- Classes HashMap, Hashtable, LinkedHashMap, and LinkedHashSet use hashing.
- An efficient hashCode() override distributes keys randomly across a wide range of buckets.
- To reiterate: if two objects are equal, their hashcodes must be equal
12. Collections
- HashMap: Fastest updates (key/value pairs); allows one null key, many null values. Hashtable: Like a slower HashMap, No null values or null keys allowed
13. Garbage Collecter
- All objects in Java live on the heap.
- The finalize() method is guaranteed to run once and only once before the garbage collector deletes an object.
- You can uneligibilize an object from within finalize().
14. Inner Class
- From code within the inner class, the keyword this holds a reference to the inner class instance. To reference the outer this (in other words, the instance of the outer class that this inner instance is tied to) precede the keyword this with the outer class name
- Technically, a static nested class is not an inner class, but instead is considered a top-level nested class.
- Instantiating a static nested class requires using both the outer and nested class names as follows:BigOuter.Nested n = new BigOuter.Nested();
15. Thread
- You can call start() on a Thread object only once. If start() is called more than once on a Thread object, it will throw a RuntimeException.
- There is no guarantee that the order in which threads were started determines the order in which they’ll run.
- A running thread may enter a blocked/waiting state by a wait(), sleep(), or join() call.
- The sleep() method is a static method that sleeps the currently executing thread. One thread cannot tell another thread to sleep.
- If not explicitly set, a thread’s priority will be the same priority as the thread that created this thread
- The yield() method may cause a running thread to back out if there are runnable threads of the same priority. There is no guarantee that this will happen, and there is no guarantee that when the thread backs out it will be different thread selected to run. A thread might yield and then immediately reenter the running state.
- The closest thing to a guarantee is that at any given time, when a thread is running it will usually not have a lower priority than any thread in the runnable state.
- When one thread calls the join() method of another thread, the currently running thread will wait until the thread it joins with has completed.
- When an object goes to sleep, it takes its locks with it.
- Static methods can be synchronized, using the lock from the java.lang.Class instance representing that class.
- All three methods—wait()/notify()/notifyAll()—must be called from within a synchronized context!
16. Primitives
- All six number types in Java are signed, so they can be positive or negative.A char is really a 16-bit unsigned integer.
- Integers can be represented in octal (0127), decimal (1245), and hexadecimal (0XCAFE).
- A char literal can also be represented as a Unicode value (‘\u0041’).
- Floating-point literals are always double by default; if you want a float,you must append an F or f to the literal.
17. Array
- When you declare an array, the brackets can be to the left or right of the variable name.
- It is never legal to include the size of an array in the declaration.You must include the size of an array when you construct it (using new) unless you are creating an anonymous array.
- Elements in an array of objects are not automatically created(remain null), although primitive array elements are given default values.
- The dimensions in a multidimensional array can have different lengths.
- Just as with array elements, instance variables are always initialized with a default value. However, Local/automatic/method variables are never given a default value.
18. Access Modifier
- Inheritance is the only mechanism for a subclass outside the package to access a protected member of its superclass
- final is the only modifier available to local variables.
- Synchronized methods can have any access control and can also be marked final.Synchronized methods cannot be abstract.
- It is legal to declare a local variable with the same name as an instance variable; this is called “shadowing.”
- Final reference variables must be initialized before the constructor completes.
- Static variables get the same default values as instance variables.
- Static methods cannot be overridden, although they can be redeclared/redefined by a subclass. So although static methods can sometimes appear to be overridden, polymorphism will not apply
- Import statements cause no performance hits and do not increase the size of your code.
19. Interface
- Interfaces can have constants, which are always implicitly public, static, and final.
- Interfaces can extend one or more other interfaces.
20. Operator
- Compound assignments (e.g. +=) perform an automatic cast.
- When floating-point numbers are divided by zero, they return positive or negative infinity.
- When the remainder operator performs a floating-point divide by zero, it will not cause a runtime exception.
- >>> fills the left bits with zeros (negative numbers will become positive).
- The && and || operators are known as short-circuit operators. No others.