In C++ this function would cause a memory leak:
Code:
void memoryleak()
{
string * s = new string;
s = "Hello World";
cout << s << endl;
}
It generates a memory leak because the string "Hello World" is allocated on the heap. Pointer 's' is created on the stack when the function is called. s points to "Hello World", but s is lost when the function returns. Basically s was the last reference to this heap memory, and now it wont be found and a memory leak occurs. Java would notice this lack of references and perform the cleanup itself, probably by calling the string destructor when all references are destroyed.