There are so many Programming Languages and here are my attempt to write down some useful note for some of them.

Upgrade to Bash 4 on Mac OSX

I came across some article regarding dictionary array for bash 4. I wanted to try it out but I am currently running bash 3.2 in Mac OSX Maverick. I came across this article on how to use homebrew to upgrade to bash 4 and ran into a few problems.

First, after I run

brew install bash

I got an error saying there is a permission error for /usr/local/share/info. I changed the owner of the folder to me and rerun the linking process.

sudo chown $(whoami) /usr/local/share/info
brew link bash

Everything seems to be working. Now I following the article and add /usr/local/bin/bash to /etc/shells and run chsh -s /usr/local/bin/bash (see link below). So I check my bash version to see what happen.

bash --version
GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin13)
Copyright (C) 2007 Free Software Foundation, Inc.

Ok. What is going on? I went and changed the Terminal Preference in Shells open with: Command (complete path) to /usr/local/bin/bash under Startup and still have the old version. Here I suspect that when I run the version check, I am not checking what the version open by the terminal but the /bin/bash version. Finally, I found out that there is an environment variable BASH_VERSION that I can check for the current bash environment that I am running.


Bingo. It works. I changed my preference back to Default login shell and still give me the update version.

Additional References:


JMH : Java Microbenchmarking Harness

I attended PJUG last night and they had a talk about microbenchmarking in Java. The talk started with what might be the most important things to keep in mind for any benchmarking code.

  • avoid dead code elimination by always return output or use blackhole
    • An example is that if the code you are benchmarking doesn’t do anything, the optimizer might interpret it as dead code and never translate that into bytecode at all. However, if the result in the dead code is being return, then optimizer won’t be able to eliminate the code away.
  • avoid constant folding by reading input from state objects
    • If at some point inside the code for benchmarking where a function is taking a constant as argument and return the same value every time , then the optimizer again will interpret the return value as a constant as well even though you might be benchmarking the execution of the function. In this case, move the constant out and make it into a field in your class so that the function argument becomes a variable.

Other interesting topic were monomorphic, bimorphic and megamorphic, and concurrency issue with jmh. The full presentation slides is here.

Additional References:

Java Koans – Intermediate

These are some notes from the Java Koans that is part of the assignment from the Advance Java Programming Class from PSU CS410J.


  • Some reference regarding boxing in Java
  • auto-boxing example: int-> Integer, long-> Long
  • auto-unboxing example: Float->float, Boolean->boolean
  • auto-unboxing a null Object will cause a NullPointerException
int x = 1;
Integer y = new Integer(x);
x == y;      // auto-unbox y and compare value
x.equals(y); // will not compile regardless what y is
y.equals(x); // auto-box x and compare value


  • List, ArrayList method add and get
  • Queue, PriorityQueue method add, peek, size, poll, isEmpty
  • Set, HashSet method add, size, contains
  • Map, HashMap method put, size, containsKey, containsValue, get.
  • Both the list and array reference to the same array “list” and if one of the element changes, the same element in the other one changes as well because of the same reference.
String[] array = {"a","b","c"};
List<String> list = Arrays.asList(array);
  • TreeMap and SortedMap. backedMap is a subMap of map which mean backedMap is a view of map. backedMap references to map and if we add something to backedMap, we are essentially adding it into map.
TreeMap<String, String> map = new TreeMap<String, String>();
map.put("a", "Aha");
map.put("b", "Boo");
map.put("c", "Coon");
map.put("e", "Emu");
map.put("f", "Fox");
SortedMap<String, String> backedMap = map.subMap("c", "f");
  • TreeSet is an ordered set and LinkedHashSet is a linked list implementation of set with predictable iteration order.


  • a.compareTo(b) return 0 if equal, 1 if a > b and -1 if a < b.
  • implements Comparable to make Objects comparable.
  • implements Comparator to impose ordering.


  • Jan 1, 1970 GMT is the origin date and time. 1L = 1 millisecond pass the origin data and time.
  • Date references.
  • add() add the value to the whole date value. e.g. July 1, 1970 and add 8 months will give March 1, 1971.
  • roll() does not change the larger value. e.g. July 1, 1970 and roll 8 months will give March 1, 1970 where year is unchanged.
  • Date format using getDateInstance().format
    • no arg or Date.format.MEDIUM: MON D, YYYY
    • Date.format.SHORT: M/D/YY
    • Date.format.FULL: DayOfWeek, Month D, YYYY
    • custom date using M=month, D=day and Y=year.


  • Object == Object compare identity
  • Object.equals(Object) compare value
  • null vs Object discussion here.
  • override equals() and hashcode(). Here is another references.


  • File createNewFile() and delete() does what it said.
  • FileWriter write() and close() are self explanatory. flush() is a Flushable that write any buffered output to the underlying stream.
  • FileReader read() and close().
  • Here we write to a file using a string (size = 22), then we created a larger char array (new char[50]) and read the file into the char array. At the end, we turn the char array back to a string. The size of the read is 22, but the size of the char array and the size of the string that created from the char array after the read is still 50 even though it only output 22 characters when println.
File file = new File("file.txt");
FileWriter fw = new FileWriter(file);
fw.write("First line\nSecond line");

char[] in = new char[50]; // <-- start with 50 empty char
int size = 0;
FileReader fr = new FileReader(file);
size =; // <-- fill up 22 char with 28 empty char left

// size of read == length of the original string
assertEquals(size, "First line\nSecond line".length());
// the size of the char array string is still 50!
assertEquals((new String(in)).length(), 50);
assertEquals("First line\nSecond line".length(), 22);
// finally the char array string has 28 empty character at the end!
assertEquals(new String(in), "First line\nSecond line"+(new String(new char[28])));


  • If OtherInner extends AboutInnerClasses, then an OtherInner object is instanceof AboutInnerClasses as well.


  • Locale with getDisplayCountry, Use with getDateInstance and getCurrencyInstance … etc.
  • Language and Country code


  • Pattern compile is the pattern to match
  • Matcher has the string to check for a pattern to match with.
    • find() return boolean of whether it find the pattern
    • start() return index of the last matched
    • “\\.” mean match a “.” period.
    • .split(delimiter) split up a string into an array of string base on the delimiter


  • Additional References regarding Serialization. 1 2 3
  • If the subclass that implements serializable has a superclass that does not implement serializable and the constructor of the superclass is invoke from the subclass, serialization will not happen.

Java Koans – Beginner – Part 2

These are some notes from the Java Koans that is part of the assignment from the Advance Java Programming Class from PSU CS410J.


  • overridden methods may returns subtype
class Parent {
  public String doStuff() { return "parent"; }
class Child extends Parent {
  // override Parent.doStuff()
  public String doStuff() { return "child"; }   
  // overload Child.doStuff()
  public String doStuff(String s) { return s; } 

abstract class ParentTwo {
  abstract public Collection<?> doStuff();

class ChildTwo extends ParentTwo {  
  // must implement doStuff()
  public Collection<?> doStuff() { return Collections.emptyList(); };


  • Downcast and Upcasting explain in here.
  • In term of the code in koans, Child extends from Parent extends from Grandparent mean child is a Child, Parent and Grandparent. However, parentReference is still a Child when upcast from Child to Parent because Child property is only hidden until being it is being downcast back to Child again.
Child child = new Child();
// upcasting always allow
Parent parentReference = child;
assertEquals(child instanceof Child, true);
assertEquals(parentReference instanceof Child, true);
assertEquals(parentReference instanceof Parent, true);
assertEquals(parentReference instanceof Grandparent, true);
  • Here, since child was originally a Child, we can downcast it back to Child with explicit downcast.
// implicit upcast
Grandparent child = new Child(); 
// explicit downcasts
Parent parentReference = (Parent) child;  
Child childReference = (Child) parentReference; 
assertEquals(childReference instanceof Child, true);
assertEquals(childReference instanceof Parent, true);
assertEquals(childReference instanceof Grandparent, true);

Child child2 = new Child()
// implicit upcast
Parent parentReference2 = chlid2;
// access Child.complain() instead of Parent.complain()
assertEquals(parentReference2.complain(), "Are we..");

// implicit upcast
Grandparent parent = new Parent();
// access Parent.complain() by explicit downcast the object back to Parent before invoking complain()
assertEquals("TPS...", ((Parent) parent).complain());


  • For nested if-else statement, else is bound to the closest if when there is no curly bracket.


  • All inherited constructors are first called before its own constructor. Example from another source as well.
class A {
  String someString = "a";
   public A() { someString += "x"; }
class B extends A {
  public B() { someString += "g"; };
public void simpleConstructorOrder(){
  assertEquals(new B().someString, "axg");


  • The private member of enum acts like the private member of inner class when access by the containing class. A better example.


  • finally is always ran! So return statement in finally will always be the return value.


  • The signature of the arguments will upcast to Object when they don’t fit. If comment out first line in class A, the first assert statement will equals to “Integer”. If comment out second line and uncomment the first line in class A, the second assert statement will equals to “int”.
class A {
  public String doStuff(int i) { return "int"; }
  public String doStuff(Integer i) { return "Integer"; }
  public String doStuff(Object i) { return "Object"; }
  public String doStuff(int...i) { return "int vararg"; }
  public String doStuff(Object...i) { return "obj vararg"; }

// assert statements
assertEquals(new A().doStuff(1), "int");
assertEquals(new A().doStuff(Integer.valueOf(1)), "Integer");
assertEquals(new A().doStuff(1l), "Object");
assertEquals(new A().doStuff(1, Double.valueOf(2)), "obj vararg");


  • full AND: &
  • short circuit AND: &&
  • write (true == a) to ensure it is a predicate and not an assignment (a = true).


  • use Arrays.equals() to compare element by element equality between two arrays.

Java Koans – Beginner – Part 1

These are some notes from the Java Koans that is part of the assignment from the Advance Java Programming Class from PSU CS410J.


  • == test if two objects are the same objects
  • equals method also test if two objects are the same
  • however, the subclass can change equals method to test if two objects are equal
Object object = new Integer(1);
Object diffObject = new Integer(1);
assertEquals(object.equals(object), true);
assertEquals(object.equals(diffObject), true);
assertEquals(object == diffObject, false);
  • new object does not equal to null


  • primitive type : its object type : size : MIN_VALUE : MAX_VALUE
    • int:Integer:32:-2147483648:2147483647
    • long:Long:64:-9223372036854775808l: 9223372036854775807l
    • short:Short:16:-32768:32767
    • byte:Byte:8:-128:127
    • char:Character:16:0:65535
    • double:Double:64:4.9E-324:1.7976931348623157E308
    • float:Float:32:1.4E-45f:3.4028235E38f
assertEquals(getType(1), int.class);
Object number = 1;
assertEquals(getType(number), Integer.class);
assertEquals(Integer.Size, 32);


  • new String() equals to “” (empty string)
  • MessageFormat allow reuse of same arguments in the token
assertEquals(MessageFormat.format("{0} {1} {0}", "a", "b"), "a b a");
assertEquals(MessageFormat.format("{0} {1} {0}", "a", "b", "c"), "a b a");
assertEquals(MessageFormat.format("{0} {1} {0}", "a"), "a {1} a");


  • toString() is the same as Object.class.getName() @ Integer.toHexString(object.hashCode())) if it is not override.


  • for loop
    • for (int j : lst) –> where lst is an array of int.
  • for loop continue with label mean execution stop in the inner loop but continue for the outerloop in this case. (count += 10 never executed). In contrast, the count = 75 without the label.
    • enter the outer for loop with i = 0
      • enter the inner for loop with j = 0
        • increment count to 1
        • repeat inner loop for j = 1, 2 and count = 1 + 2 =3
        • count > 2 so continue to outerLabel
    • repeat outer loop for i = 1, enter inner loop for j=0 with count = 4 and hit continue outerLabel again
    • do this until i = 5 (i = 2, 3, 4 and count = 4+3 = 7).
public void forLoopContinueLabel() {
  int count = 0;
  for(int i = 0; i < 5; i++) {
    for(int j = 0; j < 5; j++) {
      if(count > 2) {
        continue outerLabel; 
    count += 10;
  // What does continue with a label mean? 
  // What gets executed? Where does the program flow continue?
  assertEquals(count, 7);
  • for loop break with label mean execution leave all loop in this case and continue after the outer for loop. In contrast, count = 57 without the label.
    • enter the outer loop with i = 0
      • enter the inner loop with j = 0
        • increment count to 1
        • repeat inner loop for j = 1, 2 and count = 1 + 2 = 3
        • count > 2 so break to outerLabel and done.
    public void forLoopBreakLabel() {
        int count = 0;
        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 5; j++)
                if(count > 2) {
                    break outerLabel;    
            count += 10;
        // What does break with a label mean? 
        // What gets executed? Where does the program flow continue?
        assertEquals(count, 3);

concurrent.futures: a simple multi-thread execution

Recently I was expose to a way to launch parallel tasks in python by using concurrent.futures.  Let say you have a function call doSomething(i) that do something with a value, i and return a result. You can setup a pool of 8 threads as following:

executor = concurrent.futures.ThreadPoolExecutor(max_workers=8)

If we need to doSomething(i) on a range(64) values, we can map the result using the executor as follow:

results =, range(64))

This will return an array of 64 result. Be sure to signal the executor to free any resources by having the following line.


Additional References:


I was working with pkcs#7 padding from and was dancing around between hex() and unhexlify to convert the padding size into a one byte character.

>>>binascii.unhexlify(bytes('0'+hex(4)[2:], encoding='utf-8')

In short, I must add a string zero in front if the size is less than 16. Very ugly. Then I ran into this function chr() and ord() that does what I needed. Convert a integer value into a character and back.

>>> chr(4)
>>> ord('\x04')

Simple and clean. The only thing is to make sure what encoding is the string I am appending this at the end so I can put it into the right encoding for it.

Additional References: