Why does the following work fine?
String str; while (condition) { str = calculateStr(); ..... }
But this one is said to be dangerou
If you don't need to use the str after the while loop (scope related) then the second condition i.e.
str
while(condition){ String str = calculateStr(); ..... }
is better since if you define an object on the stack only if the condition is true. I.e. use it if you need it
condition