Amir Ashfaq wrote:
I Understand very clear Amir But what about my other 2 questions namely
2). class Dozens{int a[];} Dozens a = new Dozens(); who many objects i am creating here. 1 or 2? without instance a explicity array a.
4). class Dozens{String Name = new String("MyName");} Dozens a = new Dozens(); who many objects i am creating here. 1 or 2? how many objects may be elegible for
GC the one on the heap??
Amir Ashfaq wrote:Awesome amir i understand very clear now. thanks a lot.
Christian Daniel Ortiz Cuellar wrote:i know the question is very straightforward but a bunch of people but GC it's the topic more difficult to me so far.
Christian Daniel Ortiz Cuellar wrote:Hi Folks. i have a question about GC.
here it´s the question at book chapter 3 problem 10.
Which two are true about the objects created within main(), and eligible for garbage collection
when line 14 is reached?
A. Three objects were created
B. Four objects were created
C. Five objects were created
D. Zero objects are eligible for GC
E. One object is eligible for GC
F. Two objects are eligible for GC
G. Three objects are eligible for GC
correct answer is C and F.
i am ok with the answers but i have 4 questions.
1). here d = null; i am deleting a reference. but da[1] it´s still alive?? if this line da[1] = null; was comment 0 elements would be elegible??
2). class Dozens{int a[];} Dozens a = new Dozens(); who many objects i am creating here. 1 or 2?
3). Dozens a[] = new Dozens[3]; i am creating just one object here.
4). class Dozens{String Name = new String("MyName");} Dozens a = new Dozens(); who many objects i am creating here. 1 or 2? how many objects may be elegible for
GC the one on the heap??
i know the question is very straightforward but a bunch of people but GC it's the topic more difficult to me so far.
Thanks a Lot.
Sorry by my poor english.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Ikpefua Jacob-Obinyan wrote:
perfecto asi se hace.
hey jacob. i am still have small problems with GC but i think i am growing a little bit.
take a look at this. this is from whizlabs how many objects are elegible for GC after line 11. i think syntax is wrong missing return type but.
options
1,0,2,3
i think only one object is ready for GC which is "i" i am correct or any ideas.
other question beside. Strings i heard that integer and char also has some kind of pooling.
example.
Integer a = 127;
Integer b = 127;
System.out.println(a==b); //true
but this is also works for new Integer("127"); i am creating one on pool and one on heap.???
by the way mi correo es chiperortiz@hotmail.com thanks a lot
Lakshmi Yerr wrote:also on the first quesion instead of int [] array (int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};} ) if it has some other primitive variable (like just int or float..) then there could be 3 object created and one is eligible for garbage colleciton ..is that correct??
I assume just int i = 5; doens't create the object on teh heap??? any suggestions??
Lakshmi Yerr wrote:my understanding is you can't create Integer A[] = {1,2,3,,4,5}; its a type mismatch can't convert from int to Integer...
objects are created if you use either "new" or instantiate teh array (if it is an array)
So my answer would be there are no objects created for Integer A[] = {1,2,3,,4,5};
Lakshmi Yerr wrote:ok got you ..it compiles with java 6 ......one object created for Integer A[] = {1,2,3,,4,5}; because intialization happens.......if its just Integer a[] then no object is created on teh heap.. ..is that correct?
Lakshmi Yerr wrote:Hi Amir,
This is my first post on java forums as I am preparing for SCJP certification. I have a question on your expalnation for the follwoing lines...
line 06: da[0] = new Dozens();
line :07 Dozens d = new Dozens();
You said
line 06: Two Objects are created.. ONE: of type Dozen .. TWO: array type referenced by"dz".
line 07: Same 2 Objects (Dozen , Array ) .
On line 6 how its going to create two objects? There is only one "new" which means only one obect is created...same thing on the next line. Appreciate your help on this.
Lakshmi Yerr wrote:hi Jackzia,
creating our own diagram for this GC helps a lot...your way of picturization helps a lot and gave better understanding on the creation of object and when its availbale for GC.
here is one more question related to GC....can you explain with the diagram..how many objects are created and how many objects are eligible for GC at this comment //line 1
I drew a picture of this with 3 objects on the heap....but couldn't get how I can connect A aob reference variable to link other objects(A a = new A(); A b = new A(); A c = new A();)
class A {
A aob;
public static void main(String args[]) {
A a = new A();
A b = new A();
A c = new A();
a.aob = b;
b.aob = a;
c.aob = a.aob;
A d = new A().aob = new A();
c = b;
c.aob = null;// line1
System.gc();
}
}
Thanks,
Lakshmi
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
A d= new A().aob=new A();
In this line d is set to the assignment of (new A().aob) which inturn is set to a new Object new A ();
so the first object """new A().aob"" will be eligible for GC.
priyadharshini vijayaraghavan wrote:Hi,
In the above program, objects eligible for GC will be 2.
The one referenced by c and an another object new A().
theA d= new A().aob=new A();
In this line d is set to the assignment of (new A().aob) which inturn is set to a new Object new A ();
so the first object """new A().aob"" will be eligible for GC.
go to post "GC program needs Explanation"... The explanation to the same problem is done there.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Ikpefua Jacob-Obinyan wrote:
Lakshmi Yerr wrote:hi Jackzia,
creating our own diagram for this GC helps a lot...your way of picturization helps a lot and gave better understanding on the creation of object and when its availbale for GC.
here is one more question related to GC....can you explain with the diagram..how many objects are created and how many objects are eligible for GC at this comment //line 1
I drew a picture of this with 3 objects on the heap....but couldn't get how I can connect A aob reference variable to link other objects(A a = new A(); A b = new A(); A c = new A();)
class A {
A aob;
public static void main(String args[]) {
A a = new A();
A b = new A();
A c = new A();
a.aob = b;
b.aob = a;
c.aob = a.aob;
A d = new A().aob = new A();
c = b;
c.aob = null;// line1
System.gc();
}
}
Thanks,
Lakshmi
HINTS:
- REMEMBER THAT class A Has-a REFERENCE VARIABLE aob.
- REMEMBER THAT EVERY INSTANCE OF class A Has-a (ITS OWN) REFERENCE VARIABLE aob.
- A REFERENCE VARIABLE (to the best of my knowledge) CANNOT REFER 'DIRECTLY' TO TWO OBJECTS AT THE SAME TIME
- TWO REFERENCE VARIABLES (to the best of my knowledge) CAN REFER TO ONE OBJECT AT THE SAME TIME
VERY IMPORTANT! THOSE OF YOU THAT STUDY WITH K&B BOOK, GO TO CHAPTER 3 AND SPEND A WHOLE DAY
-IF POSSIBLE- TO READ AND UNDERSTAND 'EVERY-DETAIL' OF PAGES 184 & 185, IF YOU HAVE ANY DOUBTS,
BRING IT HERE AND LET US TRY TO PUT OUR HEADS TOGETHER AND RESOLVE THE DOUBT.
-THE DIAGRAM CLEARLY SHOWS THAT OBJECTS INICIALLY REFERED TO BY REFERENCE VARIABLES c AND d ARE ELIGIBLE
FOR GARBAGE COLLECTION
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Christian Daniel Ortiz Cuellar wrote:i heard that integer and char also has some kind of pooling.
Lakshmi wrote:also on the first quesion instead of int [] array (int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};} ) if it has some other primitive variable (like just int or float..) then there could be 3 object created and one is eligible for garbage colleciton ..is that correct??
Christian wrote:int is implicit boxed to Integer. this Integer A[] = {1,2,3,,4,5}; is perfectly legal.. as JDK 6. ilegal before JDK 6.
Christian wrote:On line 6 how its going to create two objects? There is only one "new" which means only one obect is created...same thing on the next line. Appreciate your help on this.
Lakhmi wrote:I understand how c i seligible for gor GC, but not clear hoe d is eligible for GC... d is first assinged to one object and that object is assigned to new a()..then how d is eligible for GC???
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |