• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String constructor

 
Alexan Kahkejian
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Given the code:
String s1= new String( "abc" );
Can some one tell me which documentation contains exact info about what's happening here, and if "abc" is in the string pool and so on.
I didn't found much info in JLS or API.
Thanks in advance
Alexan
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alexan,
My first reaction was to immediately yes "abc" does go into the string pool but it is a very good question. I think that there is no doubt that it must because the constructor must have a String object, which in this case is a literal and by design would be placed in the pool.
Here is the 1.4.1 implementation of that constructor:

value is a private char[] array member of String. It was intersting for me to see that this constructor doesn't always make a deep copy of the value array. But the new String will definitely be a different object.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is *all* that I could find on the subject of String literals and intern:
(1) In String.java:
public native String intern();
The intern method is implemented in platform-dependent code.
(2) In j2se\src\share\native\java\lang\String.c
Java_java_lang_String_intern(JNIEnv *env, jobject this)
{
return JVM_InternString(env, this);
}
The intern method is part of the Java Virtual Machine.
(3) JLS 3.10.5 String Literals
Each string literal is a reference (�4.3) to an instance (�4.3.1, �12.5) of class String (�4.3.3). String objects have a constant value. 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.

(4) The Java Programming Language 9.2.1 String Literal Equivalence
any two string literals with the same contents will refer to the same String object.

(5) The Java Programming Language 9.3 Utility Methods
The other utility method is intern, which returns a String that has the same contents as the one it is invoked on. However, any two strings with the same contents return the same String object from intern�

(6) Inside the Java Virtual Machine, Chapter 8, The Linking Model
Section Resolution of CONSTANT_String_info Entries
(7) Inside the Java Virtual Machine, Chapter 8, The Linking Model
Example: The Linking of the Salutation Application
Two sentences in this example and some surrounding context:
It creates and interns a new String object with the value "Hello, world!", places a reference to the string object in the constant pool entry, marks the entry as resolved, and replaces the ldc opcode with an ldc_quick.

To resolve the entry, the virtual machine creates and interns a new String object with the value "Salutations, orb!", places a reference to the new object in the data for constant pool entry three, and replaces the ldc opcode with ldc_quick.

[ May 26, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Method void m()
0 new #2 <Class java.lang.String>
3 dup
4 ldc #3 <String "abc">
6 invokespecial #4 <Method java.lang.String(java.lang.String)>
9 astore_1
10 return
A String object is allocated (new).
A reference to an interned String with contents abc is pushed onto the operand stack (ldc).
The String constructor is invoked and the reference to the interned String is passed as a parameter (invokespecial).
A reference to the new String object is stored in the variable s1 (astore_1).
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1821
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alexan Kahkejian:
Hi all
Given the code:
String s1= new String( "abc" );

It's my understanding that you've got two String objects here. One is the string literal "abc" in the pool. The other, since you're using the String constructor, is a new String object that is not in the String pool. (
Probing the String pool using == suggests I'm correct:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic