Thursday, March 29, 2012
How to apply customized code styles to Idea
To apply a customized code style first you have to put the style file to the below given path.
(C:\Documents and settings\user\)\.IdeaIC11\config\codestyles\
Then in the idea IDE go to Settings----Code style----java---global settings
Form the schema drop down list you can select the code style you have added to the above given path and apply it, :) !
Tuesday, March 27, 2012
Using String Buffer to optimize string concatenation
It is always better in performance to use String Buffer in string concatenation over using + . We can consider a code segment as follows where + is used to concat a string. If one and two a two defined strings;
String s = one+two;
This is similar to ;
String xx;
for ( int i = 0; i < name.length(); i++ )
{
xx = String.valueOf( name.charAt( i ) );
tempName = tempName + xx;
}
return tempName;
This will end up creating name.length() number of string objects in memory. If the following code segment is used instead, it will enhance the performance significantly.
String xx;
StringBuffer buf = new StringBuffer();
for ( int i = 0; i < name.length(); i++ )
{
xx = String.valueOf( name.charAt( i ) );
buf.append( xx );
}
tempName= buf.toString();
return tempName;
String s = one+two;
This is similar to ;
StringBuffer buf = new StringBuffer(one);
buf.append(two);
String s = buf.tostring();
In a situation as above, it doesn't make a difference using StringBuffer over + in string concatenation. Let's consider a situation where a string concatenation happens within a loop as given below.
for ( int i = 0; i < name.length(); i++ )
{
xx = String.valueOf( name.charAt( i ) );
tempName = tempName + xx;
}
return tempName;
This will end up creating name.length() number of string objects in memory. If the following code segment is used instead, it will enhance the performance significantly.
StringBuffer buf = new StringBuffer();
for ( int i = 0; i < name.length(); i++ )
{
xx = String.valueOf( name.charAt( i ) );
buf.append( xx );
}
tempName= buf.toString();
return tempName;
Tuesday, March 13, 2012
& Versus && and | Versus ||
&/ | - These are logical operators
&&/|| - These are short circuit operators
This means basically that,
& and | will evaluate both the right hand side and the left hand side operands of a statement and && and || will not necessarily evaluate both. If the result is confirmed, it will be short circuited.
if((x!=null) && (x.size() ==0))
if x!=null returns false we are sure that the out put is false since this is an AND operations.So if it is false the second operand will not be evaluated.So this is safe to use since the null pointer exception is avoided in this case.
if((x==null) || (x.size()==0))
if x==null returns true the whole result of is true since this is an OR operation. So the second operand will not be evaluated. And this is also safe to use since the null pointer is avoided.
if((x!=null) & (x.size() ==0))
if((x==null) | (x.size()==0))
if((x==null) && (x.size() ==0))
if((x!=null) || (x.size()==0))
&&/|| - These are short circuit operators
This means basically that,
& and | will evaluate both the right hand side and the left hand side operands of a statement and && and || will not necessarily evaluate both. If the result is confirmed, it will be short circuited.
- For an example, lets consider the following statement;
if((x!=null) && (x.size() ==0))
if x!=null returns false we are sure that the out put is false since this is an AND operations.So if it is false the second operand will not be evaluated.So this is safe to use since the null pointer exception is avoided in this case.
- Lets consider the statement
if((x==null) || (x.size()==0))
if x==null returns true the whole result of is true since this is an OR operation. So the second operand will not be evaluated. And this is also safe to use since the null pointer is avoided.
- But following statements are not safe to use since null pointer exception can occur in run time.
if((x!=null) & (x.size() ==0))
if((x==null) | (x.size()==0))
if((x==null) && (x.size() ==0))
if((x!=null) || (x.size()==0))
Subscribe to:
Posts (Atom)