1

Am trying to find the number of String objects created in the below code. I think it is 4 because Strings are immutable, so each of the first two lines in prod() method will create one object, and the third line will create 2 objects. So total 4 obejcts will be created. Could anybody clarify on that?

public class Solution {

    public void prod() {
        String str="Sku";
        str=str+"001";
        String skuId= str.substring(3,6);
        System.out.println(skuId.toString());
    }
    public static void main(String[] args) {
        new Solution().prod();
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
prabhakar Reddy G
  • 1,039
  • 3
  • 12
  • 23
  • @CodeShark I'm sure is not two :). – Luiggi Mendoza Oct 06 '15 at 14:59
  • Please edit your question and explain why do you think it's 4 (a magical number without explanation), otherwise this question is off topic. – Luiggi Mendoza Oct 06 '15 at 15:00
  • Strings are immutable, so each of the first two lines in prod() method will create one object, and the third line will create 2 objects. So total 4 obejcts will be created. – prabhakar Reddy G Oct 06 '15 at 15:02
  • @SotiriosDelimanolis Is it really a duplicate given the answer is different? – Peter Lawrey Oct 06 '15 at 15:11
  • @PeterLawrey Should I ask another question about counting `String` instances where the answers is 7? Anything that answers how to count is a duplicate to these types of questions. – Sotirios Delimanolis Oct 06 '15 at 15:12
  • @SotiriosDelimanolis it is interesting that the accepted answer is either incorrect or makes different assumptions.. – Peter Lawrey Oct 06 '15 at 15:17
  • @PeterLawrey You should downvote it and post a comment explaining why it's wrong. – Sotirios Delimanolis Oct 06 '15 at 15:18
  • @SotiriosDelimanolis a big difference is that in the previous answer you might assume that the String literals were loaded when the class was loaded (at least for Java 6 and older) but in Java 7 and in the case above, you can't assume the String literals had been loaded already and this changes the answer. – Peter Lawrey Oct 06 '15 at 15:22
  • 1
    @PeterLawrey ... I don't know what else to tell you. You've been here longer than me. We see this question 5 times a day. There are hundreds of duplicates, ten of which are in the Related section to right of the screen. If you don't think any of them apply, fine, but I think you're wrong and it's useless to keep answering them. I would prefer you add your answer to one of those instead duplicates instead, one with a lot of views. – Sotirios Delimanolis Oct 06 '15 at 15:26
  • 1
    @SotiriosDelimanolis I take you point about the question not adding a lot of value to the many questions already asked on this topic. Really, it's the wrong question to be asking a virtual machine in the first place. I have added rant here on the subject. http://vanillajava.blogspot.co.uk/2015/10/common-misconception-how-many-objects.html – Peter Lawrey Oct 06 '15 at 17:53
  • @PeterLawrey The 2500 strings is pretty great. – Sotirios Delimanolis Oct 06 '15 at 17:57
  • @SotiriosDelimanolis on my first attempt I got 5500 which is more than I thought possible. This was the lowest number I could get. – Peter Lawrey Oct 06 '15 at 18:01

2 Answers2

1

I think is 4. Below my explanation.

public class Solution {
    public void prod() {
        String str = "Sku"; // first
        str = str + "001"; // second when create "001" and third when concat strings
        String skuId = str.substring(3, 6); //fourth
        System.out.println(skuId.toString()); //method toString don't create new string
    }

    public static void main(String[] args) {
        new Solution().prod();

    }
}
  1. "Sku" //create new
  2. "001" //create new
  3. "Sku001" //result of str + "001" operations
  4. "001" //result of str.substring(3, 6). Substring method return new String()
Mateusz Korwel
  • 1,118
  • 1
  • 8
  • 14
0

In Java 7, the first time the code is run the String literals will be loaded which makes 4, but after that the prod() method would only create 2 String objects.

Before Java 7, the String literals were loaded when the class was loaded, so the method would only ever add two more Strings.

This of course leaves out the char[] in each String which is another object.

BTW, the very first time this class is loaded and run, the JVM actually creates many String objects not visible here.

For example to load the Solution class it first looks up the class name using the "Solution" String.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130