In C we know that the memory allocated locally inside a function will vanish when the function returns

Code:
char *getName()
{
char name[]="Sumit";
return name
}

The caller of the function will get a pointer to the array.Although the pointer points a garbage unknown location since the array was local, it has now been destroyed.



Now I have a doubt the same thing works in Java

Code:
public String getName()
{
String name="Sumit"; //name here is local as above;memory allocated for the object locally
return name;
}

The caller of the method will correctly get the String returned.

What is the difference in the mechanism of returning pointer in C and reference in Java?

Hope I m made my point clear.
Eagerly waiting for reply.

Regards