Thursday, June 28, 2007

How Immutable Are You?

Things are subject to change. Your weight, perspective and even someone's else impression. Good things can turn to be "not-so-good" at an instant. But things are different when it comes to Java String. Thay are "Immutable". Object created by string cannot be changed. If not needed they will simply be garbage collected. I will give you an example:

String impression = "good";
String impression = "not so ".concat(impression);

The output will be the "not so good". But what happened to "good"? The said object are lost but the real impression was never CHANGED. :)

Thursday, June 21, 2007

Defensive Coding

Defense is a method of protecting. In sports like basketball, defense is an act of protecting your opponent to hit the goal. The same is true with programming. Defensive coding is an act of preventing an error to arise or we can say bug-free. Though I believe that there is no really a bug-free application, defensive coding can minimize or even prevent a much more disaster.

So how do we do this in java? There are many ways to implement this. One for example is declaring the right modifier for the right method or class variable.

public int insertValue(){} and public synchronized int insertValue(){} really is different. Both will compile and run. But things will be different when tested in a real environment. Having a synchronized modifier prevent the method on using resources at the same time.

My team manage to learn this new skill. Hope everyone appreciate this.