Notes of “Effective Java” (Chapter 7-11)

Item 37: Use marker interfaces to define types

Marker interface contains no method declarations, which indicates some property or features. The typical marker interface is Serialize interface.

Item 38: Check parameters for validity

Check and restrict the parameters of functions.

  • Throw Exception
  • Assert

Item 39: Make defensive copies when needed

In general, if a class has mutable components that it gets from or returns to its clients, the class must defensively copy these components.

Defensive copy usually implemented by:

  • clone() method
  • copy constructor
  • static factory

This is not mandatory. If the class trusts the clients, then defensive copy could be replaced by documentation explanation to save the cost.

Read the rest of this entry »


Interesting Blogs

http://blog.codingnow.com/ 云风的博客

http://hi.baidu.com/caoz/home 百度曹政

http://coolshell.cn/ 酷壳 陈皓


Mathematics in Programming

1. 牛顿迭代法和TAYLOR 级数可以解决大部分方程求解问题,函数近似问题。

Example: sqrt()函数

public static double sqrt ( double c )
{
  if (c < 0) return Double.NaN;
  double err = 1e-15;
  double t = c;
  while (Math.abs(t - c/t) > err * t)
    t = (c/t + t) / 2.0;
  return t;
}

//QUAKE3中用的计算参数x的平方根的倒数函数,该函数的相对误差约为0.177585%

float InvSqrt (float x)
{
  float xhalf = 0.5f*x;
  int i = *(int*)&x;
  i = 0x5f3759df - (i >> 1); // 计算第一个近似根
  x = *(float*)&i;
  x = x*(1.5f - xhalf*x*x); // 牛顿迭代法
  return x;
}

2. 随机数相关问题

//Random int value drawn from discrete distribution (i with probability a[i])

public static int discrete(double[] a)
{ // Entries in a[] must sum to 1.
 double r = StdRandom.random();
 double sum = 0.0;
 for (int i = 0; i < a.length; i++)
 {
 sum = sum + a[i];
 if (sum >= r) return i;
 }
 return -1;
}

//randomly shuffle the elements in an array of  double values

public static void shuffle(double[] a)
{
 int N = a.length;
 for (int i = 0; i < N; i++)
 { // Exchange a[i] with random element in a[i..N-1]
   int r = i + StdRandom.uniform(N-i);
   double temp = a[i];
   a[i] = a[r];
   a[r] = temp;
 }
}

 


		

Notes of “Effective Java” (Chapter 1-6)

Item 1: Consider static factory methods instead of constructors

advantage:

  1. Customized meaningful names better than constructors.
  2. Associated with class instead of creating new objects.
  3. Able to return subtypes.

For the 2nd advantage, static factory methods form the basis of Service provider Framework including service interface, provider interface, provider registration API and service access API.

Item 2: Consider a builder when faced with many constructor parameters

Compare with telescoping constructors and JavaBean Style.

Builder is set as a static inner class. Builder makes the object initialized immutable.

Read the rest of this entry »


Websites with django


Some useful or interesting links

The Archive of Interesting Code: http://www.keithschwarz.com/interesting/

Web application category: http://stackparts.com/

Books, Papers and Links – Computer Graphics: http://ploobs.com.br/?p=766

Design pattern: http://www.vincehuston.org/dp/

Nice Introduction to Unicode: http://www.joelonsoftware.com/articles/Unicode.html

How to Write Doc Comments for the Javadoc Tool: http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html

HOW BROWSERS WORK: BEHIND THE SCENES OF MODERN WEB BROWSERS: http://www.html5rocks.com/en/tutorials/internals/howbrowserswork/

W3C HTML5 Specification: http://dev.w3.org/html5/spec/single-page.html

Bit Twiddling tricks: http://www-graphics.stanford.edu/~seander/bithacks.html


Vim Cheat Sheet


Coding Interview Problems

Post here for backup and looking up.

Data Structures

1. Integer
– find number of 1s
– next largest smaller
– smallest larger number
– determine if is palindrom
– itoa, atoi
– add 2 numbers w/o using + or arithmetic operators
– implement *, -, / using only +
– find max of two numbers w/o comparison
– swap two numbers with +/-
– swap two numbers with ^
– given an integer, find the closest number that is palindrome
– implement putlong() by putchar()
2. Bit array
3. Linked list
– find cycle,
– find position of cycle starts
– reverse LL
– delete a node in middle
– each node contains a value pointer pointing to a node,
duplicate LL.
– remove duplicates from sorted/un-sorted LL.
– find n-th to last node to end
– number is represented by LL, add 2 numbers Read the rest of this entry »