Why does the following work fine?
String str; while (condition) { str = calculateStr(); ..... }
But this one is said to be dangerou
One solution to this problem could be to provide a variable scope encapsulating the while loop:
{ // all tmp loop variables here .... // .... String str; while(condition){ str = calculateStr(); ..... } }
They would be automatically de-reference when the outer scope ends.