-3
class Fizz
{
    int[] nums = {1,2,3};

    int[] arr;
}

class Main
{
    public static void main(String[] args)
    { 
        Fizz[] fs = new Fizz[3];

        Fizz fizz = new Fizz();

        fs[1] = fizz;

        fizz = null;

        System.out.println("End of Logic");
    }
}

How many objects will be created during executing of this code? how many will be ready for a Garbage Collector when it reach System.out.println (“End of Logic”);? and why?

niceman
  • 2,653
  • 29
  • 57
ZacH
  • 95
  • 1
  • 5
  • 4
    None and none, because this code won't run to due to syntax errors at compile time. – azurefrog Jun 17 '16 at 20:31
  • You should show that you tried to answer this question yourself by explaining what you think the answer is and why, and what trouble understanding you're having. As is, this question isn't much better than "What's `int n = 2 + 2;`? – DavidS Jun 17 '16 at 22:22

1 Answers1

-2

Assuming the code is fixed (int[] nums = {1,2,3}; and the formatted quotations are fixed) :

4 objects will be created in total. Look through the code:

Fizz[] fs = new Fizz[3];

We create the array itself as an object, but nothing inside it has been created, so this is 1 object created.

Fizz fizz = new Fizz();

This creates 2 objects: the actual Fizz object, and the nums array inside the Fizz object.

fs[1] = fizz;

No objects are created, we just assign fs[1] to point at fizz.

fizz = null;

We assign fizz = null, but the object still exists because it is being pointed to by fs[1].

Because all objects have references, nothing will be garbage collected at that point.

Then you have the string object which was created, bringing the total to 4 objects.

  • You forgot about the "End of Logic" String literal. That's going in the string pool and not coming out. – azurefrog Jun 17 '16 at 20:35
  • he asks "when it reaches" the print statement, so I assume it hasn't been run yet and the string has not been created – questioner and answerer Jun 17 '16 at 20:36
  • The OP asks about what will be available for GC on that line, but the first question about object creation is "during executing of this code", not just up to a certain part of it. – azurefrog Jun 17 '16 at 20:37
  • what about the last part of the question, how many of these 4 will be ready for Garbage collection on print line ? – niceman Jun 17 '16 at 20:51
  • Nothing will get GC'd – questioner and answerer Jun 17 '16 at 21:07
  • Actually, all objects created before the println are eligible for GC at that point. – Sotirios Delimanolis Jun 17 '16 at 21:30
  • They will only be eligible after method terminates, which does not happen since he is asking how many will be ready on println. – questioner and answerer Jun 17 '16 at 21:40
  • No, you're talking about scope. Eligibility for GC isn't affected by scope, only by reachability within a continuing execution. By the time `println` is invoked, none of those objects is reachable by any continuing execution and they therefore become candidates for GC. – Sotirios Delimanolis Jun 17 '16 at 21:48
  • I don't know if that's true in current Java, @SotiriosDelimanolis. See [this answer](http://stackoverflow.com/a/4138213/201891) from the venerable Jon Skeet. – DavidS Jun 17 '16 at 22:25
  • @DavidS That's an implementation detail that can change in the future (as runtimes get smarter). The question here seems to ask about eligibility. See [this](http://stackoverflow.com/a/24380219/438154) answer by one of the developers of the Java language. – Sotirios Delimanolis Jun 17 '16 at 22:31