This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
This code prints out the available locales using a Numberformat object and formats a double using these locales in an extended for-loop. Per my understanding, this code creates multiple numberformat objects but 'nf2' will only refer/point to one at a time as the loop progresses. The objects that do not have a reference any longer will be garbage collected the next time gc runs. Right/wrong?
This code takes between 7-8 secs to run. Is there anyway of making it more efficient? Thanks.
Given that getCurrencyInstance is static, you're probably not creating any objects at all. Rather, nf2 is just redefined to point at a different object each time. Yes, unreferenced objects will (eventually) get gc'd.
Joined: Jan 16, 2009
John de Michele wrote: Rather, nf2 is just redefined to point at a different object each time.
John de Michele wrote:Given that getCurrencyInstance is static, you're probably not creating any objects at all.
Actually, since NumberFormat is mutable, the getXXXInstance methods all return a new object each time. It's just the pattern etc that will be set automatically.
Gina vernon wrote:And how and when are these objects created?
Inside the static methods for NumberFormat. For other classes with factory methods, the objects may be created when the class is loaded. Sometimes there is a mix between these two; Integer.valueOf is an example. For values between -128 and 127 it returns cached objects that are created when the class is loaded, for all other values it will create a new object.
The garbage collector will take care of any 'lost' (unreachable) objects, but at an unknown time.
That code completed in about a sec and I have a 900MHZ processor. I had a online flash player running, two tabs in mozilla open, eclipse IDE running, the windows explorer open, a pdf file open, and windows media player open. hahah!