dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Beginner
Apr 3, 2000

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Avoid Copying Immutable Objects Excessively

One of the most common mistakes that most less-experienced Java programmers make is the unnecessary copying of immutable objects. The problem is compounded by the fact that it is not very easy to detect through testing. Immutable objects cannot change, and hence there is no need to copy them. The most common of immutable objects is the String object. For example:
 
String s = new String("New String");
Effectively means that:
 
String temp = "New String";
String s = new String(temp);
Thus, a temporary String object is created unnecessarily, memory is allocated to it, the constructor is called, thus making the process an inefficient one. A better way would be to simply write:
 
String s = "New String";
If you must change the contents of the String at all, you must instead use StringBuffer.
C.V. Ram
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date