Asdfasf

Showing posts with label effective java. Show all posts
Showing posts with label effective java. Show all posts

Thursday, August 21, 2014

EJ-5 Avoid Creating Unnecessary Object

Ref: Effective Java by Joshua Bloch

In Item-5 of Effctive Java, Mr Bloch expresses that avoid creating unnecessary object. In essence, this requires knowing your tool, Java.

String s = new String("test"); //DON'T DO THIS

The statement creates a new String instance each time it is executed. The improved version is simply the following:

String s = "test";

This version uses a single String instance, rathe rthan creating new one each time it is executed. This idiom is called String Interning.
Here is a really interesting sample code demonstrating String interning.



First checkEquality results that both two strings are exatly same instance with same identity code and both == and equals() give true.
Second checkEquality results that two strings are different instances created with different identity codes and == results false but equals() results true.

396857438
396857438
true
true
2046236531
1294253459
true
false

Mr Bloch advices to avoid unnecessary objects by using static factory methods in preference to constructors on immutable classes. If a class preferable immutable, why you should create new instance each time instead of creating one and using it.

Boolean.valueOf(String) is almost always preferable to the constructor Boolean(String)

Boolean.TRUE is implemented inside Boolean class as a static final Boolean element:

public static final Boolean TRUE = new Boolean(true);

Wednesday, August 20, 2014

EJ-1 Consider Static Factory Methods instead of Constructors

Ref: Effective Java by Joshua Bloch

In Item-1 of Effective Java, Mr. Bloch emphasize why and how it is important to use Static Factory methods instead of constructors.

Here is goofy and wasteful way to get Boolean instances

Here is shiny and frugally way to get immutable Boolean instance with static factory method


Lets go over advantages of using static factory methods
1.) Unlike constructors, the have names. If parameters to a constructor do not describe object being returned, a static factory with well choosen name is easier to use and resulting client code easier to read.


Another example is multiple constructors with same signature as I met in stackoverflow.
Two constructors with same signature doing different things.



Can be managed by using static factory methods.



2.) Unlike constructors, they are not required to create a new object each time they're invoked. This is demonstrated as in boolean example above and important to use system resources frugally by avoiding unnecessary instance creation.

3.) A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type. Mr Bloch exemplifies this advantage with Service Provider Framework pattern.



Finally, he give a list of some common method names to statis factory methods.
  • valueOf
  • of
  • getInstance
  • newInstance
  • getType
  • newType
Often static factories are preferable, so avoid the reflex to provide public constructors without first considering static factories.