Monday, June 23, 2008

Silly mistakes!

Though Java relieves you from memory management, it doesn't mean that you don't have any memory leaks. Certain things which we use typically in our day to day coding which causes memory over usages and inefficient codes.

Tip1:
for example String s = "hi" or String s = new String("hi"); which of these 2 is good. I am voting for the first one, not just because if offers me to type less, but also more efficient. But why?

When you use new String() you unnecessary create explicit copy of the string (This makes sense only if you know what string pools are and why string is immutable?)

Tip2:
Looping though are basic constructs of any programming languages, we need to be little care full about using them. for example, java provides iterator for iterating collection.
Ex: we have linked list, it can be iterated through iterator or using get(i) function. But if you remember how linked list are implements you will realize why using get(i) is extremely inefficient.

Paying little attention while coding makes you code better.. I am still trying to learn it myself..!

No comments: