wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Objects during runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects during runtime" Watch "Objects during runtime" New topic
Author

Objects during runtime

Gina vernon
Ranch Hand

Joined: Jan 16, 2009
Posts: 108
Hi,
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.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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.

John.
Gina vernon
Ranch Hand

Joined: Jan 16, 2009
Posts: 108
John de Michele wrote: Rather, nf2 is just redefined to point at a different object each time.


And how and when are these objects created?

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Rob:

Thanks for the clarification. I guess I'll have to spend some time going through the source .

John.
Gina vernon
Ranch Hand

Joined: Jan 16, 2009
Posts: 108
Thanks.
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects during runtime
 
Similar Threads
Remove Currency Format - Can i parse back to a double?
NumberFormat
Format and NumberFormat format() method
NumberFormat does not show right currency symbol
Regional settings