3
public static void main(String [] args){ 
    String s = "java"; //line 1
    s.concat(" SE 6"); //line 2
    s.toLowerCase();  //line 3
    System.out.print(s); //line 4
 }

The answer to this question is "4". I thought it would be "3". My confusion is line 3, which creates "java" string again, but doesn't java know that the "java" string already exist in the string constant pool, so why create it again?

4
  • 1
    Strings are immutable in Java, so s.toLowerCase() returns a new String object. Commented Feb 24, 2015 at 16:14
  • 4
    @Ocracoke - Nope. He is not re-assigning the value returned by concat, so s will be "java" and then "java",toLowerCase() will return the same String "java" Commented Feb 24, 2015 at 16:23
  • @TheLostMind Ah, just looked at the code for toLowerCase() and seen that there is a return this line in it when it finds nothing to do. My bad. Commented Feb 24, 2015 at 16:37
  • @TheLostMind However, a new string object will still be created. The string itself, OTOH, should have the same address in the constant pool Commented Feb 24, 2015 at 17:37

2 Answers 2

5

3 Java Strings are created.

1. "java"  -> Goes into String constants pool // will be added if no already present
2.  " SE 6" --> Goes into String constants pool?
3. java SE 6 --> Goes on heap (call to concat)// Note : You are not re-assinging the value returned from concat() So s will still be "java"
** toLowerCase() \\ does nothing in your case since "java" is already present. toLowerCase retruns the same "java" object ( as there is no modification required to turn it into lowercase)
Sign up to request clarification or add additional context in comments.

2 Comments

Does the object returned in step 3 has references to both strings, so that is why it doesn't count as a string itself?
@SotiriosDelimanolis - I meant the same thing. The same String will be returned as there are no uppercase chars in the string. Edited the answre to make it clearer. :)
5

Java know that the "java" string already exists in constant pool, so it needs not to create the object again?

Actually, it's not "java" string that exists in the pool, but "Java" (in uppercase). If it were indeed "java", toLowerCase() would have recognized it, and returned the original string. But since the return value (i.e. "java" in all lowercase) does not match the original string (i.e. "Java" in mixed case) a new String object needs to be created, bringing the count to 4.

Edit: After the edit to the question the answer changes: now that you've changed "Java" to "java", the number of objects that get created is three, because Java String has an optimization that returns the original string from toLowerCase when the string is already in lowercase. So line 1 creates one string object "java", line 2 creates two string objects " SE 6" and "java SE 6", and lines 3 and 4 do not create any additional objects.

1 Comment

These are line numbers not how I counted the objects!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.