A good question. As mentioned by one of our friend, String s= "hello"; //will create only one object in pool String s = new String("hello");//will create two objects As far as we are using the first line(which most of us are using) for creating string object, it will be stored in Magical (String pool) which is not eligible for GC. But, as the second statement which is created in the Heap will be eligible for GC. Hope this will be clear for you.
After line 5 (i.e. when the function ends), how many objects will be eligible for gargabe collection? I think it will be 2 objects. Since as you mentioned the String str="hello" will not be stored in the heap but will be stored in string pool.