These notes are tips and points that I collected while studying for my java certifications and job interviews. Perhaps one day they will be comprehensive and organized, but for now I just need to get them down so that I can pound them into my head. If I ever hesitate at explaining whey using StringBuffer is better than using string concatenation again, I think I am just going to write it on the wiki 100 times in a row.
General Reminders
-
a class creates a default constructor only if the class has no explicit constructors
-
order of access modifiers (least to most restrictive):
public -→ protected -→ default -→ private -
notify()
method is used to tell a pool of waiting threads that one can runinterrupt()
forces a thread to leave the wait state and enter the InteruuptedException handler
notifyAll()
guarantees that all waiting objects are moved out of the waiting state
yield()
is a static method and causes the current thread to temporarily pause to let others through
thewait()
,notify()
, andnotifyAll()
methods must be executed in synchronized code -
all methods in an interface must be public (no modifier assumes public) and are also abstract
variables declared in interfaces are implicitly public, static, and final -
assertions do not enforce exceptions
-
assertions not appropriate for checking preconditions in public methods that could alter the behavior of the code when assertions are disabled
-
literal strings are placed in string pool and hence will use the same reference if contain the same value
-
arguments that are primitive type are passed by value; objects pass a copy of the reference
-
primitive sizes:
-
byte, 8 bits
-
char, 16 bits
-
short, 16 bits
-
int, 32 bits
-
float, 32 bits
-
long, 64 bits
-
double, 64 bits
-
-
hash code contract states that if two objects are equal, they must have equal hash codes (not necessarily that objects that are not equal have different hash codes)
-
start a thread with
(new Thread()).start()
-
a reader interprets 8-bit text according to the current locale (and hence not UTF-8, ONLY 8-bit text)
-
readers and writers only deal with character data
-
the only legal agruments of switch statements are enums and primitives compatible with ints
-
StringBuilder is not threadsafe
-
if a class implements Externalizable, it must have a no-args constructor
if a class implements Serializable (only), its nearest non-externalizable superclass must have a no-args constructor -
generic types are checked at compile time
-
bit shifting implicitly upgrades to an int
-
right shift x >> K equivalent to x/2K
-
left shift x << K equivalent to x * 2K
-
-
final variables can only be assigned in the constructor (or static block) and must be assigned by then
-
a call to
super()
orthis()
must proceed any other statements -
variables in the enclosing method are only accessible if those variables are marked final
-
the literal value 25.0 is a double, which must be cast to be assigned to a float variable