General

Un article de Sometimes Kitties Think Too.

SCJP

Sommaire

General points to remember



Stupid General Concepts

  1. Good encapsulation promotes good coupling

Initialization

  • Instance or static arrays are initialized to null

IO

  1. FileReaders DO NOT have a method to read up to newline character. They read chars only
  2. Beware of mixing non-generic and generic collections in the same class

Generics

  1. Map classes return null of key is not contained
  2. Can't use wildcards in type variable declaration:
  e.g.
       public static <? extends T> List<T> someMethod() { }  <--- NO

Compiler/Runtime

  1. NumberFormatException is thrown by the developer, usually when parsing strings
  2. RuntimeExceptions print after finally{} block (if one exists)
  3. instanceof operator won't compile if operands are of different types
  4. if a try/catch block calls System.exit(1); then finally{} won't be executed
  5. ClassCastException - Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. (for downcasting)
  6. the following is legal because 2 (an int literal) is promoted to a float:
   float f = 2;
  1. floating point numbers don't produce divide-by-zero ArithmeticExceptions. They only produce NaN values
  1. Compiler generated warnings about type-saftey when adding different Object types to a collection

Syntax

  1. this is a legal initialization:
   int[] arr = new int[]{1,2,3,4};
  1. a primative array can be an argument for a var-arg constructur:
  int[] f = new int[]{1,2,5} can go into void method(int... a) {}
  1. Switch statements: if method has a non-void return type then all cases should return or throw
  2. to import static methods:
       import static java.<>.<>...;


Atomic Operations

In Java, the reading and writing of 32-bit or smaller quantities are guaranteed to be atomic. By atomic we mean each action takes place in one step and cannot be interrupted. Thus, when we have multithreaded applications, the read and write operations are thread-safe and need not be made synchronized.

For example, the following code is thread safe:

 
public class ThreadSafe
{
    private int x;
    public void setX(int x)
    {
         this.x = x;
    }
}

However, we should be careful to note that the guarentee applies only to reading and writing. For eg. check the following code snippet:


public void increment()
{
        ++this.value;
}
 

Although the above snippet looks to be atomic operation, it is not. The above instruction actually consists of three instructions:

1) Read current setting of 'value'.

2) Increment the setting.

3) Write the new setting back.


Miscellaneous


  1. Every class overrides Object's toString() method
  2. StringBuffer does not override equals()
  • Enums CANNOT be extended

Wrapper Classes

  1. Wrapper classes that are given null objects throw NullPointerException not NumberFormatException
  2. Doubles are immutable
  3. String and Byte override default hashCode()
  4. an interface can extend multiple interfaces / a class can only extend 1 class


Access Modifiers & Scope

  1. Local variables can only by marked final
  2. Final variables need to be initialized either (1) when declared or (2) in the constructor
  3. only methods can be declared volatile
  4. enums can't be local to a method
  5. final methods can't be overridden
  6. the default constructor supplied by the compiler has the same access modifier as the class
  7. abstract methods cannot be synchronized

Garbage Collection

  1. finalize is never invoked more than once on an object
  1. if you override finalize you must explicity call the super's constructor
  2. LinkedHashMap can maintain entries in the order last entered or accessed:
   public LinkedHashMap(int capacity, float loadfactor, boolean accessorder);