When I declare a local variable (reference type), and instantiate it with the value null (let's say a variable that is assigned its value inside a try block, but is needed outside the try block again), is there a chance that the GC might destroy that initial reference before it is even set inside the try block, because it is initialized with null?
Or does declaring a local var like that in fact mean that there wasn't even any space allocated at all on the heap because its initial value is null, and thus the GC won't even touch it?
Setting reference variable to null does not mean that GC will reclaim the memory allocated to that variable. The job of GC is to reclaim the memory which is referred by reference variable(memory allocated to the object). As far as reference variable is considered, it will be destroyed as soon as its scope ends. So if you are doing something like this then it is 100% safe:
StringBuffer str = null;
try {
str = new StringBuffer("Test String");
// some other code
} catch(Exception e) {
}
System.out.println(str);