This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Is the answer none.

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s = "abc";
String s1 = "def";
s=null;
How many objects are eligible for GC.
Is the answer none.
can anybody kindly please answer this question???
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is none.
This is same as ur example.
class None {
public static void main(String args[]) {
String a = "abc";
String b = "def";
b =null;
}
}
Now follow the bytecode conversion of this....
class None
{
None()
{
// 0 0:aload_0
// 1 1:invokespecial #5 <Method void Object()>
// 2 4:return
}
public static void main(String args[])
{
String s = "abc";
// 0 0:ldc1 #1 <String "abc">
// 1 2:astore_1
String s1 = "def";
// 2 3:ldc1 #2 <String "def">
// 3 5:astore_2
s1 = null;
// 4 6:aconst_null
// 5 7:astore_2
// 6 8:return
}
}
Can u see any objects created on the heap. No...right. It is only String pool u can see with two string objects..."abc" and "def" which can't be garbage collected.
So, the answer is none. Am I clear with this ?
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My $0.02:



Previously I posted in a different thread here, that for this type of questions of the SCPJ2 exam the <CODE>String</CODE> objects are going to be objects created with the <CODE>new</CODE> keyword.



Let's not loose perspective of what's covered in the exam. The JVM and/or its internal workings are not necessary knowledge. Sure, it would be nice to know everything about EVERYTHING. If you can. If you cannot, it's better to concentrate at least on what's on the test
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic