Hi.....
Code :String a[] = new String(); String userName = ""; a[0]="Kalees"; userName = a[0]; a[0]=null;
My doubt is how many reference is created? and is ths array eligible for garbage collection?
Pls explain ths.....!
Printable View
Hi.....
Code :String a[] = new String(); String userName = ""; a[0]="Kalees"; userName = a[0]; a[0]=null;
My doubt is how many reference is created? and is ths array eligible for garbage collection?
Pls explain ths.....!
First a quick note: References and the actual object are different. Take for example a crime scene. There were 5 witnesses. They all say that John did it. John here is the actual object, but there were 5 references to him. The JVM's garbage collector "reclaims/deletes" objects when there are no more references to that object. So, in the previous example John did it, but since there were 0 witnesses, the cops would not have him as a suspect (given no other evidence that would say he did it).
Some syntax errors in your program:
Code :String a[];
Remember, a[] is not a valid variable name. To declare an array of strings, you must do this:
Code :String[] a;
Also, the second part of that line is incorrect. You can't create a String and say that is an array of Strings (well, in plain English, maybe but not in Java). Instead, you must do this:
Where 5 is the number of elements in the array a. Each element inside a is a reference to a string, so here there are 6 references (5 inside of a, and a itself).Code :String[] a = new String[5]
As a general rule, references don't get garbage collected, objects do. To answer your question, if the first line had been modified as I stated above, you would have 7 total references (6 from a, 1 from userName). You would have the String object "Kalees" and the String[5] object at the end of this code because the "" String was garbage collected when it was no longer referenced by userName, and no other references were pointing to it.
At this point, the String[5] array is NOT eligible to be garbage collected because it is still referenced by a, even if all it contained were null's.
What he said, sort of. You can actually do this if you like.
Code :String[] a[];
It does not matter where the brackets go however what helloworld said is the best practice. And also in your code example above if the syntax errors where fixed and that was the whole program nothing would get garbage collected. Not even if you did this at the last line.
Code :a = null;
// Json
Oh, hrm. I didn't know about that first one.
As for the second one, are you sure? I know i've had this problem when working with singly linked lists that elements "disappear' when I remove references in the wrong order. Things get garbage collected when nothing points to them, not when they point to nothing. And I thought the "" string object got garbage collected, but I have no idea how the JVM garbage collects strings (it's some weird bizarre things, though).
Well if you do variable = null on the last line in your program before the program exits the garbage collector wont be run because there is no need. It will become eligible but it wont be GCed.
How about this for array declarations.
Code :String[] a, b[], c[][], d[][][];
Ouch!
// Json
:P Then you're whole program becomes garbage collected.
hehe :D
// Json