Oracle Java tutorial - possible error at answer to question

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
16
down vote

favorite












I'm new to Java, reading Oracle tutorial.
After each section, there are questions and answers, and I don't understand a sentence within one answer (see below bolded line).



source is https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html



I'm referring to question 2, see the bolded words. As far as I understand, an array is eligible to garbage collection, if there is no reference to the array. It does not matter, whether there is a reference to the objects held by this array, as the inner objects (within the array) have their own reference counting. Is that right? Please explain the bolded sentence.



cite from oracle tutorial: https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html





  1. Question: The following code creates one array and one string object.
    How many references to those objects exist after the code executes? Is
    either object eligible for garbage collection?



    String students = new String[10];
    String studentName = "Peter Smith";
    students[0] = studentName;
    studentName = null;


    Answer: There is one reference to the students array and that array
    has one reference to the string Peter Smith. Neither object is
    eligible for garbage collection. The array students is not eligible
    for garbage collection because it has one reference to the object
    studentName even though that object has been assigned the value
    null
    . The object studentName is not eligible either because
    students[0] still refers to it.









share|improve this question


















  • 3




    Yes, it is a bug in the answer. If you care, report it :-)
    – Stephen C
    Aug 30 at 11:32










  • I did not find an email address to report to. this is the second issue, I've found. see also stackoverflow.com/questions/51634863/character-autoboxing-java
    – Eliyahu M
    Aug 30 at 11:44






  • 3




    You could submit them through the main Java Bugs Database: bugs.java.com. I can see that other people have been doing that. (But it may be a long time before they are fixed. The last major updates to the Tutorials were in 2016.)
    – Stephen C
    Aug 30 at 12:01











  • "as the inner objects (within the array) have their own reference counting. Is that right?" Just as an aside, Java doesn't use reference counting. I wrote an answer here: softwareengineering.stackexchange.com/questions/377197/… about this that explains how the JVM knows if something is garbage. I hope it helps.
    – JimmyJames
    Aug 30 at 20:23














up vote
16
down vote

favorite












I'm new to Java, reading Oracle tutorial.
After each section, there are questions and answers, and I don't understand a sentence within one answer (see below bolded line).



source is https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html



I'm referring to question 2, see the bolded words. As far as I understand, an array is eligible to garbage collection, if there is no reference to the array. It does not matter, whether there is a reference to the objects held by this array, as the inner objects (within the array) have their own reference counting. Is that right? Please explain the bolded sentence.



cite from oracle tutorial: https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html





  1. Question: The following code creates one array and one string object.
    How many references to those objects exist after the code executes? Is
    either object eligible for garbage collection?



    String students = new String[10];
    String studentName = "Peter Smith";
    students[0] = studentName;
    studentName = null;


    Answer: There is one reference to the students array and that array
    has one reference to the string Peter Smith. Neither object is
    eligible for garbage collection. The array students is not eligible
    for garbage collection because it has one reference to the object
    studentName even though that object has been assigned the value
    null
    . The object studentName is not eligible either because
    students[0] still refers to it.









share|improve this question


















  • 3




    Yes, it is a bug in the answer. If you care, report it :-)
    – Stephen C
    Aug 30 at 11:32










  • I did not find an email address to report to. this is the second issue, I've found. see also stackoverflow.com/questions/51634863/character-autoboxing-java
    – Eliyahu M
    Aug 30 at 11:44






  • 3




    You could submit them through the main Java Bugs Database: bugs.java.com. I can see that other people have been doing that. (But it may be a long time before they are fixed. The last major updates to the Tutorials were in 2016.)
    – Stephen C
    Aug 30 at 12:01











  • "as the inner objects (within the array) have their own reference counting. Is that right?" Just as an aside, Java doesn't use reference counting. I wrote an answer here: softwareengineering.stackexchange.com/questions/377197/… about this that explains how the JVM knows if something is garbage. I hope it helps.
    – JimmyJames
    Aug 30 at 20:23












up vote
16
down vote

favorite









up vote
16
down vote

favorite











I'm new to Java, reading Oracle tutorial.
After each section, there are questions and answers, and I don't understand a sentence within one answer (see below bolded line).



source is https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html



I'm referring to question 2, see the bolded words. As far as I understand, an array is eligible to garbage collection, if there is no reference to the array. It does not matter, whether there is a reference to the objects held by this array, as the inner objects (within the array) have their own reference counting. Is that right? Please explain the bolded sentence.



cite from oracle tutorial: https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html





  1. Question: The following code creates one array and one string object.
    How many references to those objects exist after the code executes? Is
    either object eligible for garbage collection?



    String students = new String[10];
    String studentName = "Peter Smith";
    students[0] = studentName;
    studentName = null;


    Answer: There is one reference to the students array and that array
    has one reference to the string Peter Smith. Neither object is
    eligible for garbage collection. The array students is not eligible
    for garbage collection because it has one reference to the object
    studentName even though that object has been assigned the value
    null
    . The object studentName is not eligible either because
    students[0] still refers to it.









share|improve this question














I'm new to Java, reading Oracle tutorial.
After each section, there are questions and answers, and I don't understand a sentence within one answer (see below bolded line).



source is https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html



I'm referring to question 2, see the bolded words. As far as I understand, an array is eligible to garbage collection, if there is no reference to the array. It does not matter, whether there is a reference to the objects held by this array, as the inner objects (within the array) have their own reference counting. Is that right? Please explain the bolded sentence.



cite from oracle tutorial: https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-answers.html





  1. Question: The following code creates one array and one string object.
    How many references to those objects exist after the code executes? Is
    either object eligible for garbage collection?



    String students = new String[10];
    String studentName = "Peter Smith";
    students[0] = studentName;
    studentName = null;


    Answer: There is one reference to the students array and that array
    has one reference to the string Peter Smith. Neither object is
    eligible for garbage collection. The array students is not eligible
    for garbage collection because it has one reference to the object
    studentName even though that object has been assigned the value
    null
    . The object studentName is not eligible either because
    students[0] still refers to it.











share|improve this question













share|improve this question




share|improve this question








edited Aug 30 at 11:58









Hulk

2,13411431




2,13411431










asked Aug 30 at 11:19









Eliyahu M

340111




340111







  • 3




    Yes, it is a bug in the answer. If you care, report it :-)
    – Stephen C
    Aug 30 at 11:32










  • I did not find an email address to report to. this is the second issue, I've found. see also stackoverflow.com/questions/51634863/character-autoboxing-java
    – Eliyahu M
    Aug 30 at 11:44






  • 3




    You could submit them through the main Java Bugs Database: bugs.java.com. I can see that other people have been doing that. (But it may be a long time before they are fixed. The last major updates to the Tutorials were in 2016.)
    – Stephen C
    Aug 30 at 12:01











  • "as the inner objects (within the array) have their own reference counting. Is that right?" Just as an aside, Java doesn't use reference counting. I wrote an answer here: softwareengineering.stackexchange.com/questions/377197/… about this that explains how the JVM knows if something is garbage. I hope it helps.
    – JimmyJames
    Aug 30 at 20:23












  • 3




    Yes, it is a bug in the answer. If you care, report it :-)
    – Stephen C
    Aug 30 at 11:32










  • I did not find an email address to report to. this is the second issue, I've found. see also stackoverflow.com/questions/51634863/character-autoboxing-java
    – Eliyahu M
    Aug 30 at 11:44






  • 3




    You could submit them through the main Java Bugs Database: bugs.java.com. I can see that other people have been doing that. (But it may be a long time before they are fixed. The last major updates to the Tutorials were in 2016.)
    – Stephen C
    Aug 30 at 12:01











  • "as the inner objects (within the array) have their own reference counting. Is that right?" Just as an aside, Java doesn't use reference counting. I wrote an answer here: softwareengineering.stackexchange.com/questions/377197/… about this that explains how the JVM knows if something is garbage. I hope it helps.
    – JimmyJames
    Aug 30 at 20:23







3




3




Yes, it is a bug in the answer. If you care, report it :-)
– Stephen C
Aug 30 at 11:32




Yes, it is a bug in the answer. If you care, report it :-)
– Stephen C
Aug 30 at 11:32












I did not find an email address to report to. this is the second issue, I've found. see also stackoverflow.com/questions/51634863/character-autoboxing-java
– Eliyahu M
Aug 30 at 11:44




I did not find an email address to report to. this is the second issue, I've found. see also stackoverflow.com/questions/51634863/character-autoboxing-java
– Eliyahu M
Aug 30 at 11:44




3




3




You could submit them through the main Java Bugs Database: bugs.java.com. I can see that other people have been doing that. (But it may be a long time before they are fixed. The last major updates to the Tutorials were in 2016.)
– Stephen C
Aug 30 at 12:01





You could submit them through the main Java Bugs Database: bugs.java.com. I can see that other people have been doing that. (But it may be a long time before they are fixed. The last major updates to the Tutorials were in 2016.)
– Stephen C
Aug 30 at 12:01













"as the inner objects (within the array) have their own reference counting. Is that right?" Just as an aside, Java doesn't use reference counting. I wrote an answer here: softwareengineering.stackexchange.com/questions/377197/… about this that explains how the JVM knows if something is garbage. I hope it helps.
– JimmyJames
Aug 30 at 20:23




"as the inner objects (within the array) have their own reference counting. Is that right?" Just as an aside, Java doesn't use reference counting. I wrote an answer here: softwareengineering.stackexchange.com/questions/377197/… about this that explains how the JVM knows if something is garbage. I hope it helps.
– JimmyJames
Aug 30 at 20:23












3 Answers
3






active

oldest

votes

















up vote
15
down vote



accepted











The array students is not eligible for garbage collection because it has one reference to the object studentName even though that object has been assigned the value null.




Yeah, that sentence is... odd. It makes no sense.



An array can be eligible for garbage collection, no matter what references it holds to other objects.



students is a reference to the array, so it's not eligible for garbage collection as long as students remains in scope.






share|improve this answer




















  • That sentence makes sense with @FarazDurrani answer
    – XtremeBaumer
    Aug 30 at 11:32






  • 3




    I don't see how @XtremeBaumer
    – Max Vollmer
    Aug 30 at 11:33

















up vote
14
down vote














Neither object is eligible for garbage collection.




It is right.



But the explanation is unclear :




The array students is not eligible for garbage collection because it
has one reference to the object studentName even though that object
has been assigned the value null.




studentName is not an object, it is a variable.

Besides, null elements in the array will not have influence on the array eligibility to be GC but it will have only on the GC eligibility of the array elements.



For example :



String students = new String[10];
// the object referenced by students is not eligible to be GC


Or :



String students = new String[10];
String studentName = "Peter Smith";
students[0] = studentName;
students[0] = null;
// no object is eligible to be GC


A correct sentence could be :



The String object is not eligible for garbage collection because the object previously referenced by the studentName variable is still referenced by the array and assigning a new object to a variable (as assigned studentName to null) changes only the reference of this variable, not these of variables that refer the same object.




Note that the array doesn't change nothing in the way which Java works with object assignment.

With a List you could notice the same behavior.

For example :



String a = "Peter";
List<String> list = ...
list.add(a);
a = null;


No object is eligible to be GC for the same reason.






share|improve this answer





























    up vote
    -5
    down vote













    It all has to do with Strings being immutable, meaning once created, they cannot be changed. So when you do this,



    String studentName = "Peter Smith";


    and then you do this,



    studentName = null,


    studnentName now points to another memory address that points to null. "Peter Smith" is still in the memory somewhere.



    After a value "Peter Smith" is assigned to student[0], student[0] still holds that value even after setting studentName to null. Because student[0] holds a reference to a place in memory that holds "Peter Smith".






    share|improve this answer


















    • 5




      I asked about the 'students' array, when it is eligible to collection. how immutable string related to this?
      – Eliyahu M
      Aug 30 at 11:36






    • 1




      "It all has to do with Strings being immutable", nope. Replacing the type String with ArrayList wouldn't change a thing for the garbage collector. You're probably refering to the fact that java is pass by value, so in this case assigning a new value to the variable of a non-primitive type does not change other references.
      – fabian
      Aug 30 at 16:37






    • 1




      The fact that String is immutable is 100% irrelevant to this example. Now, the behavior of whether or not "Peter Smith" is garbage collected is different from other types of objects, due to String interning, which is a separate concept from immutability. (But this is, as already noted, irrelevant to the question, which is about the array.)
      – Radiodef
      Aug 30 at 19:31







    • 1




      @FarazDurrani what an array contains is entirely irrelevant to whether the array is eligible for garbage collection. It only matters whether or not other things have a reference to it (in this case, students is that reference)
      – mbrig
      Aug 30 at 20:42











    • @FarazDurrani You do notice java.lang.Integer isn't mutable either? If you're referring to primitive int: they're going to the stack, so talking about them being garbage collected is pointless (of course for arrays the gc is still involved). But if you rewrite the code from the tutorial as ArrayList students = new ArrayList[10]; ArrayList studentName = new ArrayList(); students[0] = studentName; studentName = null; there would still be the reference chain students -> ArrayList instance. (In fact Strings are a bit more difficult to reason about because of the String pool.)
      – fabian
      Aug 30 at 21:00










    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52095760%2foracle-java-tutorial-possible-error-at-answer-to-question%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    15
    down vote



    accepted











    The array students is not eligible for garbage collection because it has one reference to the object studentName even though that object has been assigned the value null.




    Yeah, that sentence is... odd. It makes no sense.



    An array can be eligible for garbage collection, no matter what references it holds to other objects.



    students is a reference to the array, so it's not eligible for garbage collection as long as students remains in scope.






    share|improve this answer




















    • That sentence makes sense with @FarazDurrani answer
      – XtremeBaumer
      Aug 30 at 11:32






    • 3




      I don't see how @XtremeBaumer
      – Max Vollmer
      Aug 30 at 11:33














    up vote
    15
    down vote



    accepted











    The array students is not eligible for garbage collection because it has one reference to the object studentName even though that object has been assigned the value null.




    Yeah, that sentence is... odd. It makes no sense.



    An array can be eligible for garbage collection, no matter what references it holds to other objects.



    students is a reference to the array, so it's not eligible for garbage collection as long as students remains in scope.






    share|improve this answer




















    • That sentence makes sense with @FarazDurrani answer
      – XtremeBaumer
      Aug 30 at 11:32






    • 3




      I don't see how @XtremeBaumer
      – Max Vollmer
      Aug 30 at 11:33












    up vote
    15
    down vote



    accepted







    up vote
    15
    down vote



    accepted







    The array students is not eligible for garbage collection because it has one reference to the object studentName even though that object has been assigned the value null.




    Yeah, that sentence is... odd. It makes no sense.



    An array can be eligible for garbage collection, no matter what references it holds to other objects.



    students is a reference to the array, so it's not eligible for garbage collection as long as students remains in scope.






    share|improve this answer













    The array students is not eligible for garbage collection because it has one reference to the object studentName even though that object has been assigned the value null.




    Yeah, that sentence is... odd. It makes no sense.



    An array can be eligible for garbage collection, no matter what references it holds to other objects.



    students is a reference to the array, so it's not eligible for garbage collection as long as students remains in scope.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 30 at 11:31









    Max Vollmer

    4,91231233




    4,91231233











    • That sentence makes sense with @FarazDurrani answer
      – XtremeBaumer
      Aug 30 at 11:32






    • 3




      I don't see how @XtremeBaumer
      – Max Vollmer
      Aug 30 at 11:33
















    • That sentence makes sense with @FarazDurrani answer
      – XtremeBaumer
      Aug 30 at 11:32






    • 3




      I don't see how @XtremeBaumer
      – Max Vollmer
      Aug 30 at 11:33















    That sentence makes sense with @FarazDurrani answer
    – XtremeBaumer
    Aug 30 at 11:32




    That sentence makes sense with @FarazDurrani answer
    – XtremeBaumer
    Aug 30 at 11:32




    3




    3




    I don't see how @XtremeBaumer
    – Max Vollmer
    Aug 30 at 11:33




    I don't see how @XtremeBaumer
    – Max Vollmer
    Aug 30 at 11:33












    up vote
    14
    down vote














    Neither object is eligible for garbage collection.




    It is right.



    But the explanation is unclear :




    The array students is not eligible for garbage collection because it
    has one reference to the object studentName even though that object
    has been assigned the value null.




    studentName is not an object, it is a variable.

    Besides, null elements in the array will not have influence on the array eligibility to be GC but it will have only on the GC eligibility of the array elements.



    For example :



    String students = new String[10];
    // the object referenced by students is not eligible to be GC


    Or :



    String students = new String[10];
    String studentName = "Peter Smith";
    students[0] = studentName;
    students[0] = null;
    // no object is eligible to be GC


    A correct sentence could be :



    The String object is not eligible for garbage collection because the object previously referenced by the studentName variable is still referenced by the array and assigning a new object to a variable (as assigned studentName to null) changes only the reference of this variable, not these of variables that refer the same object.




    Note that the array doesn't change nothing in the way which Java works with object assignment.

    With a List you could notice the same behavior.

    For example :



    String a = "Peter";
    List<String> list = ...
    list.add(a);
    a = null;


    No object is eligible to be GC for the same reason.






    share|improve this answer


























      up vote
      14
      down vote














      Neither object is eligible for garbage collection.




      It is right.



      But the explanation is unclear :




      The array students is not eligible for garbage collection because it
      has one reference to the object studentName even though that object
      has been assigned the value null.




      studentName is not an object, it is a variable.

      Besides, null elements in the array will not have influence on the array eligibility to be GC but it will have only on the GC eligibility of the array elements.



      For example :



      String students = new String[10];
      // the object referenced by students is not eligible to be GC


      Or :



      String students = new String[10];
      String studentName = "Peter Smith";
      students[0] = studentName;
      students[0] = null;
      // no object is eligible to be GC


      A correct sentence could be :



      The String object is not eligible for garbage collection because the object previously referenced by the studentName variable is still referenced by the array and assigning a new object to a variable (as assigned studentName to null) changes only the reference of this variable, not these of variables that refer the same object.




      Note that the array doesn't change nothing in the way which Java works with object assignment.

      With a List you could notice the same behavior.

      For example :



      String a = "Peter";
      List<String> list = ...
      list.add(a);
      a = null;


      No object is eligible to be GC for the same reason.






      share|improve this answer
























        up vote
        14
        down vote










        up vote
        14
        down vote










        Neither object is eligible for garbage collection.




        It is right.



        But the explanation is unclear :




        The array students is not eligible for garbage collection because it
        has one reference to the object studentName even though that object
        has been assigned the value null.




        studentName is not an object, it is a variable.

        Besides, null elements in the array will not have influence on the array eligibility to be GC but it will have only on the GC eligibility of the array elements.



        For example :



        String students = new String[10];
        // the object referenced by students is not eligible to be GC


        Or :



        String students = new String[10];
        String studentName = "Peter Smith";
        students[0] = studentName;
        students[0] = null;
        // no object is eligible to be GC


        A correct sentence could be :



        The String object is not eligible for garbage collection because the object previously referenced by the studentName variable is still referenced by the array and assigning a new object to a variable (as assigned studentName to null) changes only the reference of this variable, not these of variables that refer the same object.




        Note that the array doesn't change nothing in the way which Java works with object assignment.

        With a List you could notice the same behavior.

        For example :



        String a = "Peter";
        List<String> list = ...
        list.add(a);
        a = null;


        No object is eligible to be GC for the same reason.






        share|improve this answer















        Neither object is eligible for garbage collection.




        It is right.



        But the explanation is unclear :




        The array students is not eligible for garbage collection because it
        has one reference to the object studentName even though that object
        has been assigned the value null.




        studentName is not an object, it is a variable.

        Besides, null elements in the array will not have influence on the array eligibility to be GC but it will have only on the GC eligibility of the array elements.



        For example :



        String students = new String[10];
        // the object referenced by students is not eligible to be GC


        Or :



        String students = new String[10];
        String studentName = "Peter Smith";
        students[0] = studentName;
        students[0] = null;
        // no object is eligible to be GC


        A correct sentence could be :



        The String object is not eligible for garbage collection because the object previously referenced by the studentName variable is still referenced by the array and assigning a new object to a variable (as assigned studentName to null) changes only the reference of this variable, not these of variables that refer the same object.




        Note that the array doesn't change nothing in the way which Java works with object assignment.

        With a List you could notice the same behavior.

        For example :



        String a = "Peter";
        List<String> list = ...
        list.add(a);
        a = null;


        No object is eligible to be GC for the same reason.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 30 at 12:05

























        answered Aug 30 at 11:43









        davidxxx

        56k54175




        56k54175




















            up vote
            -5
            down vote













            It all has to do with Strings being immutable, meaning once created, they cannot be changed. So when you do this,



            String studentName = "Peter Smith";


            and then you do this,



            studentName = null,


            studnentName now points to another memory address that points to null. "Peter Smith" is still in the memory somewhere.



            After a value "Peter Smith" is assigned to student[0], student[0] still holds that value even after setting studentName to null. Because student[0] holds a reference to a place in memory that holds "Peter Smith".






            share|improve this answer


















            • 5




              I asked about the 'students' array, when it is eligible to collection. how immutable string related to this?
              – Eliyahu M
              Aug 30 at 11:36






            • 1




              "It all has to do with Strings being immutable", nope. Replacing the type String with ArrayList wouldn't change a thing for the garbage collector. You're probably refering to the fact that java is pass by value, so in this case assigning a new value to the variable of a non-primitive type does not change other references.
              – fabian
              Aug 30 at 16:37






            • 1




              The fact that String is immutable is 100% irrelevant to this example. Now, the behavior of whether or not "Peter Smith" is garbage collected is different from other types of objects, due to String interning, which is a separate concept from immutability. (But this is, as already noted, irrelevant to the question, which is about the array.)
              – Radiodef
              Aug 30 at 19:31







            • 1




              @FarazDurrani what an array contains is entirely irrelevant to whether the array is eligible for garbage collection. It only matters whether or not other things have a reference to it (in this case, students is that reference)
              – mbrig
              Aug 30 at 20:42











            • @FarazDurrani You do notice java.lang.Integer isn't mutable either? If you're referring to primitive int: they're going to the stack, so talking about them being garbage collected is pointless (of course for arrays the gc is still involved). But if you rewrite the code from the tutorial as ArrayList students = new ArrayList[10]; ArrayList studentName = new ArrayList(); students[0] = studentName; studentName = null; there would still be the reference chain students -> ArrayList instance. (In fact Strings are a bit more difficult to reason about because of the String pool.)
              – fabian
              Aug 30 at 21:00














            up vote
            -5
            down vote













            It all has to do with Strings being immutable, meaning once created, they cannot be changed. So when you do this,



            String studentName = "Peter Smith";


            and then you do this,



            studentName = null,


            studnentName now points to another memory address that points to null. "Peter Smith" is still in the memory somewhere.



            After a value "Peter Smith" is assigned to student[0], student[0] still holds that value even after setting studentName to null. Because student[0] holds a reference to a place in memory that holds "Peter Smith".






            share|improve this answer


















            • 5




              I asked about the 'students' array, when it is eligible to collection. how immutable string related to this?
              – Eliyahu M
              Aug 30 at 11:36






            • 1




              "It all has to do with Strings being immutable", nope. Replacing the type String with ArrayList wouldn't change a thing for the garbage collector. You're probably refering to the fact that java is pass by value, so in this case assigning a new value to the variable of a non-primitive type does not change other references.
              – fabian
              Aug 30 at 16:37






            • 1




              The fact that String is immutable is 100% irrelevant to this example. Now, the behavior of whether or not "Peter Smith" is garbage collected is different from other types of objects, due to String interning, which is a separate concept from immutability. (But this is, as already noted, irrelevant to the question, which is about the array.)
              – Radiodef
              Aug 30 at 19:31







            • 1




              @FarazDurrani what an array contains is entirely irrelevant to whether the array is eligible for garbage collection. It only matters whether or not other things have a reference to it (in this case, students is that reference)
              – mbrig
              Aug 30 at 20:42











            • @FarazDurrani You do notice java.lang.Integer isn't mutable either? If you're referring to primitive int: they're going to the stack, so talking about them being garbage collected is pointless (of course for arrays the gc is still involved). But if you rewrite the code from the tutorial as ArrayList students = new ArrayList[10]; ArrayList studentName = new ArrayList(); students[0] = studentName; studentName = null; there would still be the reference chain students -> ArrayList instance. (In fact Strings are a bit more difficult to reason about because of the String pool.)
              – fabian
              Aug 30 at 21:00












            up vote
            -5
            down vote










            up vote
            -5
            down vote









            It all has to do with Strings being immutable, meaning once created, they cannot be changed. So when you do this,



            String studentName = "Peter Smith";


            and then you do this,



            studentName = null,


            studnentName now points to another memory address that points to null. "Peter Smith" is still in the memory somewhere.



            After a value "Peter Smith" is assigned to student[0], student[0] still holds that value even after setting studentName to null. Because student[0] holds a reference to a place in memory that holds "Peter Smith".






            share|improve this answer














            It all has to do with Strings being immutable, meaning once created, they cannot be changed. So when you do this,



            String studentName = "Peter Smith";


            and then you do this,



            studentName = null,


            studnentName now points to another memory address that points to null. "Peter Smith" is still in the memory somewhere.



            After a value "Peter Smith" is assigned to student[0], student[0] still holds that value even after setting studentName to null. Because student[0] holds a reference to a place in memory that holds "Peter Smith".







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 30 at 21:57

























            answered Aug 30 at 11:30









            Desert

            2,6182931




            2,6182931







            • 5




              I asked about the 'students' array, when it is eligible to collection. how immutable string related to this?
              – Eliyahu M
              Aug 30 at 11:36






            • 1




              "It all has to do with Strings being immutable", nope. Replacing the type String with ArrayList wouldn't change a thing for the garbage collector. You're probably refering to the fact that java is pass by value, so in this case assigning a new value to the variable of a non-primitive type does not change other references.
              – fabian
              Aug 30 at 16:37






            • 1




              The fact that String is immutable is 100% irrelevant to this example. Now, the behavior of whether or not "Peter Smith" is garbage collected is different from other types of objects, due to String interning, which is a separate concept from immutability. (But this is, as already noted, irrelevant to the question, which is about the array.)
              – Radiodef
              Aug 30 at 19:31







            • 1




              @FarazDurrani what an array contains is entirely irrelevant to whether the array is eligible for garbage collection. It only matters whether or not other things have a reference to it (in this case, students is that reference)
              – mbrig
              Aug 30 at 20:42











            • @FarazDurrani You do notice java.lang.Integer isn't mutable either? If you're referring to primitive int: they're going to the stack, so talking about them being garbage collected is pointless (of course for arrays the gc is still involved). But if you rewrite the code from the tutorial as ArrayList students = new ArrayList[10]; ArrayList studentName = new ArrayList(); students[0] = studentName; studentName = null; there would still be the reference chain students -> ArrayList instance. (In fact Strings are a bit more difficult to reason about because of the String pool.)
              – fabian
              Aug 30 at 21:00












            • 5




              I asked about the 'students' array, when it is eligible to collection. how immutable string related to this?
              – Eliyahu M
              Aug 30 at 11:36






            • 1




              "It all has to do with Strings being immutable", nope. Replacing the type String with ArrayList wouldn't change a thing for the garbage collector. You're probably refering to the fact that java is pass by value, so in this case assigning a new value to the variable of a non-primitive type does not change other references.
              – fabian
              Aug 30 at 16:37






            • 1




              The fact that String is immutable is 100% irrelevant to this example. Now, the behavior of whether or not "Peter Smith" is garbage collected is different from other types of objects, due to String interning, which is a separate concept from immutability. (But this is, as already noted, irrelevant to the question, which is about the array.)
              – Radiodef
              Aug 30 at 19:31







            • 1




              @FarazDurrani what an array contains is entirely irrelevant to whether the array is eligible for garbage collection. It only matters whether or not other things have a reference to it (in this case, students is that reference)
              – mbrig
              Aug 30 at 20:42











            • @FarazDurrani You do notice java.lang.Integer isn't mutable either? If you're referring to primitive int: they're going to the stack, so talking about them being garbage collected is pointless (of course for arrays the gc is still involved). But if you rewrite the code from the tutorial as ArrayList students = new ArrayList[10]; ArrayList studentName = new ArrayList(); students[0] = studentName; studentName = null; there would still be the reference chain students -> ArrayList instance. (In fact Strings are a bit more difficult to reason about because of the String pool.)
              – fabian
              Aug 30 at 21:00







            5




            5




            I asked about the 'students' array, when it is eligible to collection. how immutable string related to this?
            – Eliyahu M
            Aug 30 at 11:36




            I asked about the 'students' array, when it is eligible to collection. how immutable string related to this?
            – Eliyahu M
            Aug 30 at 11:36




            1




            1




            "It all has to do with Strings being immutable", nope. Replacing the type String with ArrayList wouldn't change a thing for the garbage collector. You're probably refering to the fact that java is pass by value, so in this case assigning a new value to the variable of a non-primitive type does not change other references.
            – fabian
            Aug 30 at 16:37




            "It all has to do with Strings being immutable", nope. Replacing the type String with ArrayList wouldn't change a thing for the garbage collector. You're probably refering to the fact that java is pass by value, so in this case assigning a new value to the variable of a non-primitive type does not change other references.
            – fabian
            Aug 30 at 16:37




            1




            1




            The fact that String is immutable is 100% irrelevant to this example. Now, the behavior of whether or not "Peter Smith" is garbage collected is different from other types of objects, due to String interning, which is a separate concept from immutability. (But this is, as already noted, irrelevant to the question, which is about the array.)
            – Radiodef
            Aug 30 at 19:31





            The fact that String is immutable is 100% irrelevant to this example. Now, the behavior of whether or not "Peter Smith" is garbage collected is different from other types of objects, due to String interning, which is a separate concept from immutability. (But this is, as already noted, irrelevant to the question, which is about the array.)
            – Radiodef
            Aug 30 at 19:31





            1




            1




            @FarazDurrani what an array contains is entirely irrelevant to whether the array is eligible for garbage collection. It only matters whether or not other things have a reference to it (in this case, students is that reference)
            – mbrig
            Aug 30 at 20:42





            @FarazDurrani what an array contains is entirely irrelevant to whether the array is eligible for garbage collection. It only matters whether or not other things have a reference to it (in this case, students is that reference)
            – mbrig
            Aug 30 at 20:42













            @FarazDurrani You do notice java.lang.Integer isn't mutable either? If you're referring to primitive int: they're going to the stack, so talking about them being garbage collected is pointless (of course for arrays the gc is still involved). But if you rewrite the code from the tutorial as ArrayList students = new ArrayList[10]; ArrayList studentName = new ArrayList(); students[0] = studentName; studentName = null; there would still be the reference chain students -> ArrayList instance. (In fact Strings are a bit more difficult to reason about because of the String pool.)
            – fabian
            Aug 30 at 21:00




            @FarazDurrani You do notice java.lang.Integer isn't mutable either? If you're referring to primitive int: they're going to the stack, so talking about them being garbage collected is pointless (of course for arrays the gc is still involved). But if you rewrite the code from the tutorial as ArrayList students = new ArrayList[10]; ArrayList studentName = new ArrayList(); students[0] = studentName; studentName = null; there would still be the reference chain students -> ArrayList instance. (In fact Strings are a bit more difficult to reason about because of the String pool.)
            – fabian
            Aug 30 at 21:00

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52095760%2foracle-java-tutorial-possible-error-at-answer-to-question%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery