PDA

Click to See Complete Forum and Search --> : overwriting classes in loops


quipy
Apr 30th, 2003, 03:57 AM
hey ya'll!
what happens if:

Date d = new Date();
StringTokenizer st = new StringTokenizer();

...both of these lines are in a while loop... what would happen when the loop is passed the second/third etc time?

with variables, if you declare them outside the loop, their values will be overwritten throughout the loop. but what about those two? i don't mind having "d" (in Date) overwritten; it's just that at every pass in the loop, i need to pass different arguments into it. ie.

1st pass: Date d = new Date(2003, 12, 12);
2nd pass: Date d = new Date (2001, 6, 6);

would declaring them in the loop be okay or would i have to do so before the loop? and if the latter is true, how do i pass new arguments into "d" if the constructor line is outside the loop?

eh?

CornedBee
Apr 30th, 2003, 05:23 AM
It will create an incredible amount of objects floating around.

quipy
Apr 30th, 2003, 06:15 AM
any way to "clear" them in code?

CreoN
Apr 30th, 2003, 07:22 AM
CornedBee
wouldn't the garbage collector take care of the objects that were created in the loop for each iteration? So after the first iteration the objects are removed...

quipy
In the Date class you have methods for setting year, month and day, so you don't need to create a new date object in each iteration. Just change the values of a date object already defined outside the loop.

CornedBee
Apr 30th, 2003, 10:29 AM
The problem isn't memory consumption, the garbage collector takes care of that. The problem is the overhead of creating objects and collecting them.

Dillinger4
Apr 30th, 2003, 11:55 PM
Im pretty sure the the Garabage Collector runs in a low priority background thread so there is no guarantee that the objects will get collected in the order that they were created or at all for that matter. Automatic garabage collection should not be preceived as a license for uninhibited creation of objects and forgetting about them. Im sure CornedBee would agree. :D

CornedBee
May 1st, 2003, 04:49 AM
Absolutly. The garbage collector runs when there's nothing else to do, when memory gets low or when it is explicitly invoked using System.gc();