• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instances of string objects

 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s1 = "abc";
String s2 = new String("abc");

How many string objects have been created in the memory?. Is it 2 or 3
 
Satish SN
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The answer is 3 strings

reason:

if we declare String str = "abc";
it creates string object in the string pool

if we declare String str2 = new String("def");
it creates one string object in the string pool
and also u r 'new' keyword it creates another object in the jvm heap
 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sathish.

I do agree. But here in the example which i had given previously, i'm refering the same string literal "abc".
[ July 28, 2005: Message edited by: Periakaruppan Thiagarajan ]
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously 2...s1 and s2.

The only difference is that s1 is a String literal and is initialized directly whereas s2 is constructed by passing the value "abc" as a parameter to the constructor of the class String.

However both are String objects.
 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since, i'm refering to same literal "abc" which is already in string constant pool before JVM executes the 2nd line, expected answer is 2. but the answer is 3. Is there any way to verify it?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm quite sure that 2 is the correct answer. As far as I know, there is no easy way to verify this, though. You could try to use a profiler, but as the JVM will also create some Strings under the hood at startup, that probably won't be easy...
 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ilja Preuss.
Will you tell me how many string objects will be created if i give some other literal like "def" instead of "abc"?
 
Satish SN
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As people have different opinions on the no. of string objects

key point to notice :

if the case is String str = "abc";
String nstr = new String("abc");

in this scenario comming to the string pool if it finds the string "abc" in the pool it doesn't create a new object instead it points to the existing string in the pool in that case the objects created would be 2 only
 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks kumar. As you said, it must be 2 if both the string literals are same(eg."abc"). Suppose instead of using String class, if i use StringBuffer class
String s1 = "abc";
StringBuffer sb1 = new SttringBuffer("abc");
Will they point to the same String literal which is already existing in the String literal pool?. If yes, then any modification to variable sb1 in turn will affect s1 indirectly. So are we in the right direction?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Periakaruppan Thiagarajan:
Thanks kumar. As you said, it must be 2 if both the string literals are same(eg."abc"). Suppose instead of using String class, if i use StringBuffer class
String s1 = "abc";
StringBuffer sb1 = new SttringBuffer("abc");
Will they point to the same String literal which is already existing in the String literal pool?. If yes, then any modification to variable sb1 in turn will affect s1 indirectly. So are we in the right direction?


The String *literals* ("abc") will both refer to the same String object. In the second line, that object will be passed to the constructor of the StringBuffer *which will make a copy of the content of the String* (in the form of a char array). The StringBuffer will then "forget" about the String totally - changes to it will only be represented in its internal representation (which isn't a String), so they will not reflect in the original String object.

Hope this helps - you might also want to look at the source code of StringBuffer, which comes with the JDK.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose instead of using String class, if i use StringBuffer class
String s1 = "abc";
StringBuffer sb1 = new SttringBuffer("abc");
Will they point to the same String literal which is already existing in the String literal pool?.

No. The objects referred to by s1 and sb2 will be two separate objects on the heap.

If yes, then any modification to variable sb1 in turn will affect s1 indirectly.
As mentioned before, no, but you should also know that String objects (literals included) are immutable; they can't be changed. (StringBuffers, of course, are mutable)

It might also help to read the following: String literals
[ July 28, 2005: Message edited by: Steve Morrow ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic