Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
m wasimullah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s="abc";
s=s.concat("def");
In the above code, the object representing "abc" will become eligible for garbage collection or will stay in string pool.


Thanks in advance.
 
sadasiva kumar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A local variable lives only within the method that declared the variable.
An instance variable lives as long as the object does.If the object is still alive,so are its instance variables.

Here the variable 's' is in scope only within its method.So 's' becomes eligible for garbage collection.
 
deepak adlakha
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"s" wont be eligible for garbage collection as its still pointing to new object which is created. "abc" will be eligible for GC, if we dont have any more reference in string pool referening to this.
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GC runs only on the objects on the heap to make memory available as needed.
"abc" is created in the string constact pool.GC does not run there.
So "abc" will not be garbage collected.

As a side note garbage collection with strings is not in the exam.
 
ZHU Yinli
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we say that the objets which are created by new is in the pool, the others will be collected?
 
Kevin Liu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by megha joshi:
[QB]

So "abc" will not be garbage collected.

[QB]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic