Sunday, August 21, 2011

What the difference in cunstruct String instance with and without new keyword

There are two different approach of how to create a new String instance. The first one uses keyword new, the second one without new.

For example:

String a = "Iron Man";
String b = new String("Andre");

There is a slight difference between these two approaches, and it is related to String pool memory. In the example above the first variable "a" will be created in string pool memory, and second "b" will be created in "non-pool" memory. Read more about this in linked post below:

Inheriting Java: Chapter 32: The String Class

About memory leak with substring() in class String

I've found a good post about memory leak that may happen during use of method substring() in class String.