3

I know that at compile time when a String is created, that String will be THE string used by any objects of that particular signature.

String s = "foo"; <--Any other identical strings will simply be references to this object.

Does this hold for strings created during methods at runtime? I have some code where an object holds a piece of string data. The original code is something like

for(datum :data){
    String a = datum.getD();  //getD is not doing anything but returning a field

    StringBuffer toAppend = new StringBuffer(a).append(stuff).toString();

    someData = someObject.getMethod(a);
    //do stuff

}

Since the String was already created in data, it seems better to just call datum.getD() instead of creating a string on every iteration of the loop.

Unless there's something I'm missing?

3 Answers 3

7

String instances are shared when they are the result of a compile-time constant expression. As a result, in the example below a and c will point to the same instance, but b will be a different instance, even though they all represent the same value:

String a = "hello";
String b = hell() + o();
String c = "hell" + "o";

public String hell() {
   return "hell";
}

public String o() {
   return "o";
}

You can explicitly intern the String however:

String b = (hell() + o()).intern();

In which case they'll all point to the same object.

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

1 Comment

I was not aware of .intern() This answers the grander question I was driving at.
3

The line

 String a = datum.getD();

means, assign the result of evaluating datum.getD() to the reference a . It doesn't create a new String.

5 Comments

Yeah I think he knows that. Read the second last sentence.
@Mark - maybe he does, maybe he doesn't. It is hard to tell what his code is supposed to mean ... and hence what he is really asking about.
@Stephen: "it seems better to just call datum.getD() instead of creating a string"...seems he knows quite clearly that datum.getD() doesn't create a string. He said so himself.
@Stephen: I will give you that it's confusing as to what the OP's alternative would be in this case.
Since this is in a for loop, ever iteration a new reference is created to the string in datum.getD() My thinking is that I already have what I want in my data collection. Why create a new reference every iteration instead of just calling for my data?
-2

You are correct that strings are immutable so all references to the same string value use the same object. As far as being static, I do not think Strings are static in the way you describe. The Class class is like that, but I think it is the only object that does that.

I think it would be better to just call the datum.getD() since there is nothing that pulling it out into its own sting object gains for you.

If you do use the datum.getD() several times in the loop, then it might make sense to pull the value into a String object, because the cost of creating a string object once might be less than the cost of calling the getD() function multiple times.

1 Comment

You can definitely have two String instances that represent the same string. So your first sentence is wrong. Also: datum.getD() most likely doesn't create a new String object each time it's called (especially if it's a normal getter as its name implies).

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.