Tuesday, June 24, 2014

Java Coding Style

Using this as a defensive measure.
Labeling Closing Braces
while(notYet != done)
{
// many lines of code go here

} // while(notYet != done)

The Infamous Double Equals
the compiler isn’t smart enough to realize that
boolean thisValue = true;
if(thisValue = true)
put all constant expressions on the left-hand side for consistency because it can be all too easy to overlook the typographical error.

Monday, June 16, 2014

Cleaner Code: Using JDK Class

char str[] = s.toCharArray();
int[] set = new int[256];
Arrays.fill(set, -1);


System.out.println(Arrays.toString(array));
or if your array contains other arrays as elements:
System.out.println(Arrays.deepToString(array));

Arrays.fill(array, 1.0);
Collections.sort(intervals, new IntervalComparator());


Thursday, June 12, 2014

Learning JDK Source Code

java.util.AbstractCollection.toString()
    public String toString() {
        Iterator<E> it = iterator();
        if (! it.hasNext())
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (;;) {
            E e = it.next();
            sb.append(e == this ? "(this Collection)" : e);
            if (! it.hasNext())
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
    }

java.util.Arrays.deepEquals(Object[], Object[])
    public static boolean deepEquals(Object[] a1, Object[] a2) {
        if (a1 == a2)
            return true;
        if (a1 == null || a2==null)
            return false;
        int length = a1.length;
        if (a2.length != length)
            return false;

        for (int i = 0; i < length; i++) {
            Object e1 = a1[i];
            Object e2 = a2[i];

            if (e1 == e2)
                continue;
            if (e1 == null)
                return false;

            // Figure out whether the two elements are equal
            boolean eq = deepEquals0(e1, e2);

            if (!eq)
                return false;
        }
        return true;
    }

    static boolean deepEquals0(Object e1, Object e2) {
        assert e1 != null;
        boolean eq;
        if (e1 instanceof Object[] && e2 instanceof Object[])
            eq = deepEquals ((Object[]) e1, (Object[]) e2);
        else if (e1 instanceof byte[] && e2 instanceof byte[])
            eq = equals((byte[]) e1, (byte[]) e2);
        else if (e1 instanceof short[] && e2 instanceof short[])
            eq = equals((short[]) e1, (short[]) e2);
        else if (e1 instanceof int[] && e2 instanceof int[])
            eq = equals((int[]) e1, (int[]) e2);
        else if (e1 instanceof long[] && e2 instanceof long[])
            eq = equals((long[]) e1, (long[]) e2);
        else if (e1 instanceof char[] && e2 instanceof char[])
            eq = equals((char[]) e1, (char[]) e2);
        else if (e1 instanceof float[] && e2 instanceof float[])
            eq = equals((float[]) e1, (float[]) e2);
        else if (e1 instanceof double[] && e2 instanceof double[])
            eq = equals((double[]) e1, (double[]) e2);
        else if (e1 instanceof boolean[] && e2 instanceof boolean[])
            eq = equals((boolean[]) e1, (boolean[]) e2);
        else
            eq = e1.equals(e2);
        return eq;
    }

Notes on JDk8

Joining strings with a delimiter is finally easy: String.join(", ", a, b, c) instead of a + ", " + b + ", " + c.
• Integer types now support unsigned arithmetic.
• The Math class has methods to detect integer overflow.

Friday, June 6, 2014

JSZip Demo: Creating zip files from remote file