Thursday, August 27, 2009

Java Tips

Java General Tips

 Unicode Characters are expressed as 4 character hex codes e.g. c = "\u45c7" .
 Scientific numerical literals such as 1E2 are assigned to double data types by default.
 The default type of any numeric literal with a decimal point is double.
 integer and long operations / and % can throw ArithmeticException whilst float / and % do not - not even with divide by zero.
 Negative binary numbers are the same as their positive equivalents 1 , with bits reversed.
 ( -1 >> anything ) = -1 and is always promoted to at least int type.
 ( -1 & anything ) = anything, since 1 is all 1 bits.
 The &sim operator is actually a typo in some books for "~" (bitwise integral inversion).
 In range literal assignments are allowed for byte , short and char primitive data types.
 The Math.random() method produces a number >= 0.0 and < 1.0 .
 Angles passed to Math.sin(), cos() or tan() are expressed in Radians.
 The Math class is immutable.
 The expression -0.0 == 0.0 returns true.
 Valid boolean literals are true¡± and false¡± only.
 The expression for ( ; ; ) { } is an infinite loop therefore it will compile.
 Static methods are called once at class load time.
 Static methods can not be overridden, nor can final methods.
 Methods may not have the same name as the constructor(s).
 Constructors CAN be private, as in the Math class - but it is not common.
 Constructors can throw ANY exception.
 Initializer blocks are executed in the order of their declaration.
 Instance initializers get executed ONLY IF the objects are constructed.
 The main() method can be declared final.
 Static methods can not use non-static features of the class. The main() method can not reference unqualified non-static methods.
 You can access and use static methods and variables e.g. Math.round() and Math.PI.
 Access modifiers dictate which classes NOT instances may access features.
 Access modifiers are only used on class level variables, with rare exceptions.
 (Local) Inner classes in methods can access final variables inside the enclosing method or its parameters. They may also access the non-static data of the enclosing class.
 Methods of a static inner class can not access instance variables of the enclosing class, only static ones.
 Inner classes can have any access modifier, but those defined within a method can not. Such a class is private to that block. Inner classes in a block may not be static.
 No inner class can have a static member.
 Garbage collection takes place (possibly) sometime after an object is no longer being used. Assign null to a variable when you have finished using it.
 A method of the form : public Object pop() { return storage[index--]; } will cause a memory leak.