will two strings with same content be stored in the same memory location?

前端 未结 9 1118
慢半拍i
慢半拍i 2020-11-27 19:29

This is a question that I got in an interview.

I\'ve two strings defined as

String s1=\"Java\";
String s2=\"Java\";

My question i

相关标签:
9条回答
  • 2020-11-27 20:00

    When compiler optimizes your string literals, it sees that both s1 and s2 have same value and thus you need only one string object. It's safe because String is immutable in Java.

    String s1="Java";
    String s2="Java";
    System.out.println(s1== s2);
    

    This gives result true because s1 and s2 points to the same object.

    String Pool is the mechanism that all already defined string are stored in some 'pool' and before creating new String object compiler checks if such string is already defined.

    0 讨论(0)
  • 2020-11-27 20:00
    String s1="Java";
    String s2="Java";
    

    both points to same object. for more detail click here

    0 讨论(0)
  • 2020-11-27 20:06

    YES, Andrew Hare was answer on stack overflow in this link https://stackoverflow.com/a/2486195/4835894.

    Basically, a string intern pool allows a runtime to save memory by preserving immutable strings in a pool so that areas of the application can reuse instances of common strings instead of creating multiple instances of it.

    0 讨论(0)
  • 2020-11-27 20:07
    String s1="Java";
    String s2="Java";
    My question is whether these two references point to the same memory location  
    

    Dumb citing §3.10.5 of Java Language Specification:

    A string literal is a reference to an instance of class String (§4.3.1, §4.3.3).

    Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

    And read the comments to code example there:

    This example illustrates six points:

    • Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).

    • Literal strings within different classes in the same package represent references to the same String object.

    • Literal strings within different classes in different packages likewise represent references to the same String object.

    • Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.

    • Strings computed by concatenation at run time are newly created and therefore distinct.

    • The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.

    0 讨论(0)
  • 2020-11-27 20:08

    When you have

    String str1 = new String("BlaBla");  //In the heap!
    String str2 = new String("BlaBla");  //In the heap!
    

    then you're explicitly creating a String object through new operator (and constructor). In this case you'll have each object pointing to a different storage location.

    But if you have:

    String str1 = "BlaBla";        
    String str2 = "BlaBla";
    

    then you've implicit construction. Two strings literals share the same storage if they have the same values, this is because Java conserves the storage of the same strings! (Strings that have the same value)

    0 讨论(0)
  • 2020-11-27 20:09
    String s1="Java";
    String s2="Java"; 
    

    Do they point to the same memory location?

    I originally said "no" but in the case above, see the StringPool answer referred to below, it's actually yes..

    "when we create identical strings (without new keyword), does the content get stored in the memory only once and all the String objects with the same content just refer to the same location"

    ...kind of see detailed answer in question "Java Strings and StringPool"

    "The hash codes of s1 and s2 are the same. But are hashcodes dependent directly on memory location of the object?"

    No the hashcodes depend on the content of the String

    0 讨论(0)
提交回复
热议问题