1

With new operator String create the string in heap and put a copy in string const pool so the result of hashcode is same in below case;

  String s1 = new String("Test");
   String s2 = new String("Test");
   System.out.println(s1.hashCode() + " "+ s2.hashCode() + " " + s1.equals(s2));

But without using new operator its still giving the same hashcode

String s1 = new String("Test");
    String s2 = "Test";
    System.out.println(s1.hashCode() + " "+ s2.hashCode() + " " + s1.equals(s2));

Then what is the differnce between above two notation of string creation although they are referening to same string in string const. pool

dead programmer
  • 4,223
  • 9
  • 46
  • 77

4 Answers4

1

Based on Effective java.

It is often appropriate to reuse a single object instead of creating a new function- ally equivalent object each time it is needed. Reuse can be both faster and more stylish. An object can always be reused if it is immutable. As an extreme example of what not to do, consider this statement:

String s = new String("stringette"); // DON'T DO THIS!

The statement creates a new String instance each time it is executed, and none of those object creations is necessary. The argument to the String construc- tor ( "stringette" ) is itself a String instance, functionally identical to all of the objects created by the constructor. If this usage occurs in a loop or in a frequently invoked method, millions of String instances can be created needlessly.

The improved version is simply the following:

String s = "stringette";

This version uses a single String instance, rather than creating a new one each time it is executed. Furthermore, it is guaranteed that the object will be reused by any other code running in the same virtual machine that happens to con- tain the same string literal

therefore creating unnecessary new Object of String or any other Objects are expensive.

Secondo
  • 451
  • 4
  • 9
0

From docs and Java.lang.String class, Internal Implementation of hashCode()

/** * Returns a hash code for this string. The hash code for a * String object is computed as *

     * s[0]*31^(n-1) + s1*31^(n-2) + ... + s[n-1]
     * 
* using int arithmetic, where s[i] is the * ith character of the string, n is the length of * the string, and ^ indicates exponentiation. * (The hash value of the empty string is zero.) * * @return a hash code value for this object. */
public int hashCode() {
int h = hash;
    int len = count;
if (h == 0 && len > 0) {
    int off = offset;
    char val[] = value;

        for (int i = 0; i < len; i++) {
            h = 31*h + val[off++];
        }
        hash = h;
    }
    return h;
}

More about String hashcode here

Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0

Both expression gives you String object, but there is difference between them. When you create String object using new() operator, it always create a new object in heap memory. On the other hand, if you create object using String literal syntax e.g. String s2 = "Test"; it may return an existing object from String pool (a cache of String object in Perm gen space, which is now moved to heap space in recent Java release), if it's already exists. Otherwise it will create a new string object and put in string pool for future re-use.

for further reading see:here

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
-1
 String str = new String("String");

always create a new object on the heap.

Here creates a new String having for value the value of the constant "String" and assignates its reference to the variable str.

String str = "String";

uses the String pool

Here assignates the reference associated to the constant "String" to the variable str

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55