|
-
Apr 30th, 2003, 02:57 AM
#1
Thread Starter
Lively Member
overwriting classes in loops
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?
-
Apr 30th, 2003, 04:23 AM
#2
It will create an incredible amount of objects floating around.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 30th, 2003, 05:15 AM
#3
Thread Starter
Lively Member
any way to "clear" them in code?
-
Apr 30th, 2003, 06:22 AM
#4
Lively Member
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.
-
Apr 30th, 2003, 09:29 AM
#5
The problem isn't memory consumption, the garbage collector takes care of that. The problem is the overhead of creating objects and collecting them.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 30th, 2003, 10:55 PM
#6
Dazed Member
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.
-
May 1st, 2003, 03:49 AM
#7
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();
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|