- enum declaration outside class cannot contain static, final or any access modifiers, although enum type is implicity static and final. However enum declaration inside class can be declared as static(although implicitly static) but explicit declaration as final will be compiler error. Explicit declaration of static or final for enums outside class is also compiler error.
- Two special cases where primitive == doesn't match the object equals method. NaN and +0.0f with -0.0f. For first case, primitive == is false(so equals will return true) and for second case primitive == is true(so equals will return false).
- final instance variables need to get initialized before the constructor ends.(else compiler error).
- Protected member inherited to subclass can be accessed in the subclass by a member of declared type as subclass but not by a member of declared type as superclass. Inherited protected member will not be accessible by other classes in the same package of subclass(unless they also extend the subclass).
- ArrayStoreException : Storing a object in a array of another object type. E.g., Object[] a = new String[5]; a[0] = new Integer(1); here storing a integer in a array object(whose runtime type is String array causes Heap Pollution).
- Compile type determines which overloaded method to be invoked. Run time type determine which overridden method to be invoked.
- Constructor is never inherited and hence no overriding. static methods are inherited but can not be overridden.
- result of expression is integer. so, byte b = 7+3; will be compile error. Needs explicit cast. However for compound assignment, explicit cast is not required. Thus, byte b = 127; b += 3; will run fine and will give output as -126.
- local variable initialization before use. Even null check on a local variable cannot be done before the local variable is initially set as null. Thus, Date d; if(d == null) will cause variable not initialized error. Initialization within if condition or for loop which the compiler is not certain will be executed and then accessing the variable later down the line will also cause variable not initialized error.
- When initializing multi-dimension array, the second size can be omitted. However, before assigning the elements into the second dimension array, it has to be initialized with the size of the second dimension. i.e., each of the first dimension array should be assigned a new array object with size specified in the new constructor.
- auto-unboxing may result in NPE. e.g., passing wrapper objects to method which expects a primitive and that the wrapper object passed is a instance member variable not initialized.
- overloading between methods is fine between one which takes a primitive and other which takes the wrapper of primitive.
- In switch statement, case labels should be compile time constants(final variable should be initialized in same line when declared). Duplicate case label will be a compile time error. case label constants should be within the range of the switch argument otherwise compile error.
Friday, September 22, 2006
Some more SCJP5 tips
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment