Java Study Note

Static Variable is Evil

A static variable is essentially a global variable (in Java sense). All methods in the program can potentially change the state of a static variable, which makes it hard to reason and control. The predictability of the overall program is then impaired.

Optional Type

Optional type is created to avoid null return value of method call. Since in Java null is not a sub-type of any other type, return a null would raise a nullpointer exception which crashes the problem.

Time Complexity of Java Collection Methods

| Arraylist | LinkedList

get(index) | O(1) | O(n)
add(E) | O(n) | O(1)
add(E, index) | O(n) | O(n)
remove(index) | O(n) | O(n)
Iterator.remove() | O(n) | O(1)
Iterator.add(E) | O(n) | O(1)

Threading Overhead

Time cost for creating a new thread: 1 ms - 8 ms

Parallel Effectiveness Index:

Tc = Thread Creation Cost
Tr = Single Threading Runtime
N = Number of Threads

Tr = (nn/(n-1))Tc

Threading is beneficial for a program starting from 18 ms (running time) for Intel i7 Process (8 hardware thread)

Static Member and Static Context

  1. Non-static field can not be referenced from a static context
  2. Non-static methods can not be referenced from a static context
  3. Static field can be referenced from a non-static context
  4. Static methods can be referenced from a non-static context
  5. Static member is not recommended to be accessed via instance reference

Java Classpath

Java classpath are search path of java classes/jar/properties. Only .jar file path and directory path can be taken as the classpath (as the same in C/C++ header search path). Multiple classpaths are separated by colon (:). For classpath of jar files, a wildcard (*) is provided to represent all the jar files in a directory.

Java Null Pointer

A java null pointer belongs to the null type, which has no name. Null in java can be considered as a keyword/literal to represent null reference. Null is not a subtype of anything, nor does it is the supertype of anything.