Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

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



Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

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.



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