I don't recommend spending a great deal of time and energy in making the program more elegant, unless by elegant you mean simpler. The graders will not appreciate extra complexity. My advice is to do the simplest thing that will fulfill the requirements. You are, of course, free to disregard that advice.
Originally posted by Arun Kumar:
Ok, let me write down what i understand from my limited knowledge of garbage collection. Please correct me if i am wrong.
In the first case, the while loop never ends. And the Object tmp created will never be elligible for garbage collection. Unless there is a break; inside the while loop and the method holding this while block returns.
In the second case the Object created inside the while loop again will never be garbage collected, unless there is a break in the while loop. Here when the execution comes out of the while loop (may be beause of break , you are explicitly setting the Object reference to null, so the object is immediately elligble for garbage collection (need not wait untill the method holding the while block returns).