Home arrow static arrow Java Programming [Archive] - how does collection at background work in java?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - how does collection at background work in java?
1 Duke Stars available
This topic has 25 replies on 2 pages.    1 | 2 | Next »

Posts:31
Registered: 6/21/04
how does collection at background work in java?  
Jun 24, 2004 9:05 AM



 
inside a method, i.e.
void myMethod()
{
String s=new String("Hello");
//..................
}

s is a local variable, does java auto clean the varaible after the method has been called?
i mean, if VM adds the line at last s=0; or something similar to this to clean the variable at once after calling method to clean the variable?
does java leave it now and clean the variable when applicatin exits?

cheers
 

Posts:6,750
Registered: 1/25/04
Re: how does collection at background work in java?  
Jun 24, 2004 9:10 AM (reply 1 of 25)



 
Here's a [url http://www-106.ibm.com/developerworks/java/library/j-jtp11253/]good article[/url] on how Java collects garbage.
 

Posts:31
Registered: 6/21/04
Re: how does collection at background work in java?  
Jun 24, 2004 9:17 AM (reply 2 of 25)



 
Hi,
article is good to be read later, currently i hope simple answer "Yes" or "No"

cheers
 

Posts:6,750
Registered: 1/25/04
Re: how does collection at background work in java?  
Jun 24, 2004 9:19 AM (reply 3 of 25)



 
s is a local variable, does java auto clean the
varaible after the method has been called?

No, variables are not collected only objects.

i mean, if VM adds the line at last s=0;

No.

or something
similar to this to clean the variable at once after
calling method to clean the variable?

No.

does java leave it now and clean the variable when
applicatin exits?

Maybe.
 

Posts:24,036
Registered: 2/3/03
Re: how does collection at background work in java?  
Jun 24, 2004 9:24 AM (reply 4 of 25)



 
Hi,
article is good to be read later, currently i hope
simple answer "Yes" or "No"

cheers

If you can't be bothered to do a little research on your own, you should consider paying somebody for a commercial support contract.

cheers.
 

Posts:835
Registered: 2/12/01
Re: how does collection at background work in java?  
Jun 24, 2004 9:26 AM (reply 5 of 25)



 
Java gc removes unreferenced object. In the loacal procedure, references are hold on the stack. The stack shrinks when you return making objects not accessible; thus, nulling is not needed.

BTW, new String("something"); is a static object thus it will not be GCd until class is loaded.
 

Posts:31
Registered: 6/21/04
Re: how does collection at background work in java?  
Jun 24, 2004 9:39 AM (reply 6 of 25)



 
i just finished my exam, i have got big headach from articles!

cheers
 

Posts:6,750
Registered: 1/25/04
Re: how does collection at background work in java?  
Jun 24, 2004 9:58 AM (reply 7 of 25)



 
BTW, new String("something"); is a static object thus
it will not be GCd until class is loaded.

Where did you come up with this?
 

Posts:835
Registered: 2/12/01
Re: how does collection at background work in java?  
Jun 24, 2004 11:09 AM (reply 8 of 25)



 

Where did you come up with this?
new String("a") == new String("a"). This is because there is only one static instance "a".
 

Posts:1,577
Registered: 00-02-25
Re: how does collection at background work in java?  
Jun 24, 2004 11:23 AM (reply 9 of 25)



 
It would help if you actually wrote a program before posting ... here, try running this and tell me what you get:
public class Tester{    public static void main(String[] argv)    throws Exception    {        System.out.println(new String("a") == new String("a"));    }}
 

Posts:1
Registered: 1/18/04
Re: how does collection at background work in java?  
Jun 24, 2004 11:23 AM (reply 10 of 25)



 

Where did you come up with this?
new String("a") == new String("a"). This is because
there is only one static instance "a".

System.out.println(new String("a") == new String("a"));

prints:

false
 

Posts:31,095
Registered: 4/30/99
Re: how does collection at background work in java?  
Jun 24, 2004 11:25 AM (reply 11 of 25)



 

Where did you come up with this?
new String("a") == new String("a"). This is because
there is only one static instance "a".

The word "static" has a particular meaning in Java. Using it in other contexts can lead to confusion.

And anybody who really wants to know whether new String("a") == new String("a") returns true or false need only run a tiny test program to find out, instead of believing people who post here. (My bet is that the tiny test program prints "false".)
 

Posts:4,680
Registered: 6/14/99
Re: how does collection at background work in java?  
Jun 24, 2004 1:49 PM (reply 12 of 25)



 
Where did you come up with this?
new String("a") == new String("a"). This is because there is only one static instance "a".

Do you mean that same String values are added to the String pool? If you do, then in that case your example is one way that it will NOT look in the pool. Perhaps you mean:
  String a = "a", b = "a";  System.out.println( a == b );
 

Posts:835
Registered: 2/12/01
Re: how does collection at background work in java?  
Jun 24, 2004 2:09 PM (reply 13 of 25)



 
Exactly, I was sure that

String a = "a"
and
String b = new String("a")

are equivalent statements.

I do not know about string pool but "a" is declared/used in the class. When there is no need in that class, it is unloaded and constants like "a" should be removed from memory as well. That is why I've called them "static".
 

Posts:31,095
Registered: 4/30/99
Re: how does collection at background wiki/./work in java?  
Jun 24, 2004 2:16 PM (reply 14 of 25)



 
Exactly, I was sure that

String a = "a"
and
String b = new String("a")

are equivalent statements.

But now you know better, right?
 
This topic has 25 replies on 2 pages.    1 | 2 | Next »