aspose file tools*
The moose likes Beginning Java and the fly likes Instances of string objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instances of string objects" Watch "Instances of string objects" New topic
Author

Instances of string objects

Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65
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

Joined: Apr 19, 2005
Posts: 70
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


Satish SN<br />SCJP 1.4 & SCWCD 1.4
Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65
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

Joined: Jun 29, 2005
Posts: 128
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.


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65
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

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65
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

Joined: Apr 19, 2005
Posts: 70
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

Joined: Jul 26, 2005
Posts: 65
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: May 22, 2003
Posts: 657

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instances of string objects
 
Similar Threads
String Object
velmurugan notes (arrays)
How many string objects created??
StringBuffer Question.
Strings in java.