12/26/2017

My Development Principals in Java World

Here are my development principals in java world. 

  • Simple and Small objects
  • Immutable object
  • Interface, it’s a contract that our object must obey in order to communicate with other objects.
  • Don’t use static object and method
  • Don’t use null anywhere
  • Multiple contractor to reuse your code-fee
  • Always chain exception
  • Aspect-oriented programming for special handling
  • A unit test is a part of class.
  • ...continue...

9/18/2014

Switch JDK verson on Mac

When you installed many of jdk versions, there are two ways to switch the jdk.

  • Using Java Preferences application, but Apple decided to remove it.
  • Using java_home in console

After a lot of googing a found simple way solution, I’d like to share.

Edit your profile /.bash_profile or /.zshrc

alias java_ls='/usr/libexec/java_home -V 2>&1 | grep -E "\d.\d.\d[,_]" | cut -d , -f 1 | colrm 1 4 | grep -v Home'

function java_use() {
export JAVA_HOME=$(/usr/libexec/java_home -v $1)
export PATH=$JAVA_HOME/bin:$PATH
java -version
}

Execute java_ls

1.8.0_11
1.7.0_45
1.6.0_65-b14-462
1.6.0_65-b14-462

Execute java_use 1.8

java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

3/17/2014

Different Between Reader/Writer and InputStream/OutputStream

A Reader/Writer are designed for character-oriented streams. If the information we are reading is all text, then the Reader will take care of character decoding for you. For example, you can read all in text file to represent the characters in Java.

An InputStream/OutStream are designed for byte-oriented streams. It grabs/writes the data byte by byte without performing any kind of translation. For example, you can read/write image data, mp3…or any binary files.

3/14/2014

Observer Pattern

The observer pattern is a behavioral design pattern which defines a one-to-many relationship between objects. When the observable object changes its state, all dependent objects are notified and updated automatically. In stock market case, a stock trading system keeps track of all trades of specific stocks (subjects) and must inform all trader (observers) when a new trade has been made for new prices.

Word Definitions

Observer

someone who sees or notices something

Observable

something that is observable can be seen or measured

Code Example

Event-Bus

To be honest, I don’t want the Observable class to be tightly coupled with the classes that are interested in observing it. It doesn’t care who it is as long as it fulfils certain criteria. I think we might consider the event-bus if there are a lot of usage in your system. There are two reasons :

  • It replaces the tight coupling introduced by explicit listeners with a flexible loosely coupled design.
  • An event bus can be thought of like the observer pattern with an extra layer of decoupling.

Reference Link

3/13/2014

Communicate between Threads in Java

The Java wait-and-notify mechanism is a way for inter thread communcation in Java. It allows one thread to communicate to another thread that a particular condition has occured. The wait-and-notify mechanism cannot be used to replace the synchronized mechanism and does not solve the race condition problem. In fact, wait-and-notify must be used in conjunction with the synchronized lock to prevent a race condition.

Just like the synchronized mechanism, the wait-and-notify mechanism is available from every object. There are three methods to invoke on any object should have the object monitor.

wait, notify and notifyAll methods in Java doc

  • wait()

    The current thread must own this object’s monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object’s monitor to wake up either through a call to the notify method or the notifyAll method.

  • notify()

    Wakes up a single thread that is waiting on this object’s monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object’s monitor by calling one of the wait methods.

  • notifyAll()

    Wakes up all threads that are waiting on this object’s monitor.

On the other question, wait and notify method are used to put thread on wait but they are not in Thread class along with Sleep() or Join() method instead they are declared in Object class. This article has few reasons which make sense on Why Wait and Notify is in Object Class in Java.

Code for Producer-and-Consumer Problem

3/10/2014

Turn Off LinkedIn Activity Broadcast

If you want to search the new job and don’t want your employer to know that you’re updating your profile, I suggested to turn off your activity broadcasts. It’s a good idea to avoid annoying messages. Here’s how to turn off your activity broadcasts.

3/09/2014

Comparable and Comparator in Java

The comparable and Comparator are two interfaces provided by Java Core API. They used to compare stuff in some way for sorting and ordering objects in Java. But what they exactly are and what is different between them? In this tutorial we will see their usage.

Comparable interface

Comparable interface has only one method called compareTo and its purpose is to define the natural ordering of an object. Specifically, compareTo() method should need to return an integer as follows:

  1. a negative integer (–1) if this object is less than the specified object
  2. a positive integer (1) if this object is greater than the specified object
  3. zero integer (0) if this object is equal to specified object

For example consider the real case where we have a Name class:

Comparator interface

Comparator interface gives you the capability to sort a given collection any number of different ways to pas to methods such as Collections.sort() or Arrays.sort(). So you might want to create a Comparator object for the following ideas.

  1. To provide service different ways to sort your objects.
  2. To provide comparison methods for classes that you cannot modify or no control over such String, Date …
  3. To implement strategy pattern for representing an algorithm in which situation to be used.

Sometimes, to write the comparaTo() or compare() methods are very bored and the code will also not be readable. Guava ComparisonChain allows you to check whether an object is less-than or greater-than another object by comparing multiple properties. It should be used the both of Comparable and Comparator.