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.

1
  • 2
    @NPE I'd ask such question only to understand the language well. Commented Apr 13, 2013 at 9:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@AmitG: I think the current answer addresses every point of your question.
Can you please refer a link for "The current versions don't do that anymore". Anyways I am googling it now...
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.

1 Comment

be it any possible literal, the logic will be same.
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 :-)

3 Comments

it is not a generalized answer, try adding String str3 = "World";
please see my updated question. I have change "World" to "Java" again.
Check the updated example, specifically str4 and the String API.

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.