Sunday, June 11, 2006

Learnings from javaranch

1. Unreachable statement compile error is only for the loop statements(while) and not for conditional statement(if). while(false){..} will cause code not reachable error. But if(false){..} will not cause error. the conditions of course should be compile time constants.

2. "final variable in for loop". for(final int i:some iterable){print i..} will not error because the enhanced for loop is rewritten in basic for loop as for(int j=0; i < iterator.count;j++){final int i = iterator.next()...} so, the scope of variable is within one loop of the for. Instead within the for loop, if we tried to modify the final variable, it will error.

3. static variable and static fields are initialized in the order in which they are declared. so in the static block if we assign a variable which is declared down the line to another variable it will error. However this doesn't hold good for static methods. In the static methods, the variable which are declared down the line can be still be accessed and assigned to. Issue only for static initializers.

4. Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals. Strings computed by concatenation at run time are newly created and therefore distinct.

5. a private method in super class and same method name in child class. If some other method in parent class invoked by the child object calls the private method, it is the private method of the super class which is called and not the polymorphic method in child. polymorphic invocation doesn't apply if the super class method is private.

No comments: