1

There are two ways to create String object.

String str = new String("Java");
String str2 = "Java";

I know in first case constructor will definitely be invoked. But not aware about second one. Will constructor be invoked?

String substr = new String(str.substring(2,str.length));  // str is new object
String substr2 = new String(str2.substring(2,str2.length)); //str2 is not with new keyword

want to make sure substr and substr2 are same kind of operation and same behaviour in the heap memory.

I know one thing that String.substring() doesn't create new object at all but uses previous char[] object with different offset.

so what happen with substr and substr2? Can we relate these things with constructor as offset is generated inside constructor.

AmitG
  • 10,365
  • 5
  • 31
  • 52

3 Answers3

3

From the JLS §3.10.5. String Literals:

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.

Thus, "Java" refers to a String object. Furthermore, any string literal consisting of the same charactes would refer to the same String object. However, how and when this object is constructed is unspecified.

want to make sure substr and substr2 are same kind of operation and same behaviour in the heap memory.

Yes, they are exactly the same kind of operation.

I know one thing that String.substring() doesn't create new object at all but uses previous char[] object with different offset.

This is unspecified. What you describe is how older versions of Oracle's JDK worked. Current versions don't do that anymore: substring() now copies the characters. This change was made in Java 7u6.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • @AmitG: I think the current answer addresses every point of your question. – NPE Apr 13 '13 at 10:03
  • Can you please refer a link for "The current versions don't do that anymore". Anyways I am googling it now... – AmitG Apr 13 '13 at 10:19
2

constructor is invoked when any object is created.

In case of String str2 = "World";, jvm will first search for the string "world" in the string pool. the constructor will be called only if this literal doesn't exist in pool, otherwise it will return the existing object.

Ankit
  • 6,554
  • 6
  • 49
  • 71
0

The constructor should always be invoked. To find out for yourself debug this application:

public static void main(String[] args) {
  String str = new String("Java"); // <-- breakpoint here
  String str2 = "World";
  String str3 = "Java"; // (1)
  String str4 = new String("Java"); // (2)

  System.out.println(str + " " + str2 + " " + str3 + " " + str4);
}

Set the breakpoint on the first line (String str = new...) and "Step into" to go into the String() constructor. Then "Step out" of the constructor, "Step over" to the next line and "Step into" again. You should be in the String() constructor again.

1: As for str3 it should come from the pool, i.e. if (str3 == str) will be true.

2: But str4 will be a new instance, i.e. if (str4 == str || str4 == str3) will be false. See the String API.

Let us know what you found :-)

Cebence
  • 2,406
  • 2
  • 19
  • 20