• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC

 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ,
I have one question about garbage collection
------------------
1.String s="hello";
2.s=null;
-----------------
1.String s=new String(hello");
2.s=null;
-----------------
In both cases will s be eligible for garbage collection after
executing line 2?
coz I read in one of mock exams that in 1st case it is not eligible
for garbage collection.
Any ideas...?
Thanks
-PC
 
Donny Nadolny
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey. I'm pretty sure that in the first case it will be eligable for garbage collection. After all, when you create a string using the keyword new, it just says yuo want a new string created, even if there's already a string in the pool with the same text. When you just say String s = "whatever";, it will need to create a new string if there isn't one already, and it should be garbage collected if nothing points to it.
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that they are saying that the first is not eligible for garbage collection, because String constants are held in the string pool. It's a bad question because the reference is lost, but the underlying "object" (invisible to the developer) is not collected....
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI - for the purpose of the exam,
1- You WILL need to know when non-String objects are eligible for GC -BIG and TRICKY
2- You WILL need to understand that Strings are immutable - that's BIG
3- A very minor, tiny portion, kind of, on the String pool
4- You WON'T need to understand how GC and the String pool work together!!!
My advice is, the String pool is a tiny speck on the exam, don't worry about it until you have GC, threads, and inner classes TOTALLY nailed. If you read people's results they often say GC and threads hurt their scores - I can't remember the last time someone said that the String pool was any problem at all.
-Bert
 
Unni Kainila
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bert,
From some previous readings at Javaranch, I had the same impression about the String-Pool vs GC from an exam point of view. Then I read some other posting and some mock exams go very detail on the subject....I get nervous
Your tips are really a relief.
As I am inching close to the exam date, I am focusing more on Threads, InnerClasses and Collections. I feel very comfortable about GC, Inheritance(overriding/overloading), Exception, and ofcourse Fundamentals.
Bert, is that a safe approach ?
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unni -
I think your approach is good
I obviously can't tell you if you are prepared or not, I think there are a lot of threads about comparing mock results to real results and that's probably the best way to go.
I will say that since Kathy and I work with the Sun certification team we have an advantage when it comes to knowing where to focus your attention. I think that since Sun's objectives are a bit vague, other mock exam writers tend to err on the side of overkill. That of course makes a lot of sense. It explains why some mocks may cover certain topics more than necessary. Dan Chisolm's mocks are highly regarded and quite extensive, and I believe he has created a subset that more accurately reflects what's really on the exam. You can find Dan's comments about this on many threads.
My only other comment would be that of all the areas you mentioned I think Collections is the least important for the exam. If you understand the various class's capabilities, when to use which, and iterator basics you should be in good shape.
Good luck and let us know how you do!
-Bert
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So going back to the question
-----------------
1.String s="hello";
2.s=null;
-----------------
1.String s=new String(hello");
2.s=null;
-------------------
Should I assume that in both cases s is eligible for GC after excuting line 2?
Thanks,
-PC
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic