Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Constructor for String

Status
Not open for further replies.

AdamRice32

Programmer
Dec 31, 2001
38
US
Hi All,

Does anyone know offhand if there's any post-compilation difference between the following:

Code:
    String a = "Some string.";

and

Code:
    String b = new String( "Some string." );

I would assume that the byte code would be the same for these two, but I'm curious if anyone knows otherwise.

Thanks,
Adam Rice
 
Yes there is a slight difference.

Code:
String a = "Some string.";
This code looks in the JVMs String list and if that exact String has already been created than it will not be created again. The reference for a will be made to point at the String already in memory.

Code:
String b = new String( "Some string." );
This code will ALWAYS create a new String in memory.

A good example is to run the following:
Code:
String a = "Some String.";
String b = "Some String.";
String c = new String("Some String.");

if (a == b) {
  System.out.println("a is the same object as b.");
}
else {
  System.out.println("a is not the same object as b.");
}

if (a.equals(b)) {
  System.out.println("The values of a and b are equal.");
}
else {
  System.out.println("The values of a and b are not equal.");
}
if (a == c) {
  System.out.println("a is the same object as c.");
}
else {
  System.out.println("a is not the same object as c.");
}

if (a.equals(c)) {
  System.out.println("The values of a and c are equal.");
}
else {
  System.out.println("The values of a and c are not equal.");
}

This code will output:
Code:
a is the same object as b.
The values of a and b are equal.
a is not the same object as c.
The values of a and c are equal.

Seems kind of odd at first doesn't it? Someone was mentioning the SCJP exam, this is exactly the type of thing to find on the exam.
 
Thanks wushutwist... I see how that works now. Apparently Java does some kind of pooling in order to conserve references to equal objects. I'm guessing that this is particular to Strings, though.

A follow-up question would be: is using the String constructor that takes a String as an argument actually allocating twice the memory (albeit only temporarily) for the String? That is, once to create the original String that's the argument for the constructor, and then again for the constructor because it's actually copying the contents of the String literal passed in?

Any thoughts?

Thx,
Adam
 
I can't answer you absolutely either way without doing more testing but my gut instinct says yes. The literal is probably being created (if it is not already in memory) and then a new String is being created from the literal and the reference is being set.

Again, I have not done testing to determine if this is the case or not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top