An alternative to the deep copy technique

I have this task once where I have to deep copy a very complex object. I can't use Object.clone() since the Class is not implementing Cloneable and I just want to leave the Class alone. I found this solution which uses Java serialization to deep copy an object and lucky for me the object's Class implements serializable. The only drawback of this solution is its use of resources -- the process of serializing and deserializing takes much time compared to using Object's clone method.

Posted at at Wednesday, September 09, 2009 on 9/9/09 by Posted by amj | 0 comments   | Filed under: , , ,

Lessening NullPointerException Occurance

Most of the time NullPointerException is hard to avoid but you can lessen its occurrence on your application. For example you are comparing a String object to a String literal

Don't do this


String lCountry = getCountry();
if(lCountry.equals("Philippines")){
//Do Something
}


Do this instead


String lCountry = getCountry();
if(("Philippines").equals(lCountry)){
//Do Something
}


The first implementation could lead to a null pointer exception if getCountry() returned null. Note that on both cases you are creating a String Object with value Philippines so doing it the other way around will avoid avoid NullPointerException.

There is another way to do this by using apache commons StringUtils.equals() which is null safe.

Eclipse Keyboard Shorcut

Probably one of the most used statement in Java is the print statement

System.out.println("myfavoritestatement")
and when you are using eclipse as your IDE there is a shortcut for this statement

type syso then Ctrl + Space Bar

other useful keyboard shortcuts is enumerated here

Posted at at Monday, July 20, 2009 on 7/20/09 by Posted by amj | 0 comments   | Filed under: ,

Java Ternary Operator

You can use these shortcuts for better readability when you have lots of if-else statements

A = B == C 
This is read as:
If B = C then A is true, else A is false
The statement above is the same as this if-else statement:
if(B == C){
A = true;
} else {
A = false;
}
The other one has a more familiar form than the previous:
A = A ? B : C
This is read as:
If A is true it will take the value of B, If A is false else A will be C.
The statement above is the same as this if-else statement:
if(A == true){
A = B;
} else {
A = C;
}
Note: A,B, and C should return a boolean value.

Posted at at Monday, July 20, 2009 on by Posted by amj | 0 comments   | Filed under: ,

Java Variable Naming

I am not sure if this is the best practice for naming variables but I find the codes easier to read this way. Append the letter 'a' to every Class variables, 'p' to method/Constructor parameter, and 'l' to local variables .

If you do this I'm sure the guy who will maintain your code will not have a hard time debugging your code.

Class Animal {
private String aAnimalType;
public void setAnimalType(String pAnimalType){
aAnimalType = aAnimalType;
}
public String getAnimalType(){
return aAnimalType;
}
}

Posted at at Friday, July 17, 2009 on 7/17/09 by Posted by amj | 0 comments   | Filed under: , , ,

Eclipse Shortcut

In the eclipse shortcut you could specify the workspace, the virtual machine and the heap size -- this is where your object resides.

To specify this parameters, right click the eclipse shortcut then go select properties. The parameters are specified on the Target input box



Below is an example of the parameters you could specify on the target input box

 C:\Dev\Tools\Eclipse\3.0\eclipse.exe -data C:\Dev\Eclipseworkspace -vm %JAVA_HOME%\Bin\javaw.exe -vmargs -Xms512m -Xmx768m 
So let us dissect and explain the parts
The first parameter is where the eclipse executable file is located and this is the default parameter specified in the input box
 C:\Dev\Tools\Eclipse\3.0\eclipse.exe 
The second parameter is where the workspace is located.
 -data C:\Dev\Eclipseworkspace 
The third parameter is the location of your java virtual machine
 -vm %JAVA_HOME%\Bin\javaw.exe 
and the last parameter is the value of your heap size
-vmargs -Xms512m -Xmx768m 
The vmargs I'm using before was -Xms256m -Xmx512m but I'm always getting an OutOfMemory error so I increase it to -Xms512m -Xmx768m and this solves the problem. The first and second argument is the minimum and maximum memory respectively. Intuitively the values that I assign in the vmargs is a multiple of 256 (i.e. 256, 512, 768, 1024 etc) and the maximum memory I assigned is less than my computer's physical memory.

Posted at at Thursday, July 16, 2009 on 7/16/09 by Posted by amj | 0 comments   | Filed under: , , , , ,

When to create a new method

I think this is one of the question that they don't teach in training camps - when to you create a new method.

For code reuse, you should put a piece of code in a new method if some Class/method other than the method you are in is also performing that routine or if you anticipate that this piece of code will be use by other classes in the future.

Utility Class and Utility Method

Most of the application that I'm working right now has Utility Classes wherein you put your utility methods. A utility method is a method that doesn't need Class instance variable to operate and they are usually declared as public and static.

public static String
stripHeader(String pRawXML)
{
//implementation
}


Code Readability

For code readability and easy maintenance I choose to create a new method if that block of code performs a certain task even if it is not reused by my other classes.

Posted at at Thursday, July 16, 2009 on by Posted by amj | 0 comments   | Filed under: , , ,