wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Objects" Watch "String Objects" New topic
Author

String Objects

Kaushik Badiyani
Greenhorn

Joined: Jun 07, 2001
Posts: 27
String s1,s2,s3,s4;
s1="abc";
s2=s1;
s3=s2+"zzz";
s4=s3;
How many String objects are created ??
I think it is 2.
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
yes thats right .... 2 string objects are created
1. s1 and s2 are references to one of the String object
2. s3 and a4 are the references to the other String object


hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
Igor Gaschits
Greenhorn

Joined: Jul 27, 2001
Posts: 20
Nope, Kaushik.
No String object created. These are all String literals. They reside in the String pool. And be careful, they are not available for GC, because they are not objects. To create String object we use
String name = new String("Kaushik");
HTH,
Igor


Igor Gaschits,<br />JSCP
Akhil Gupta
Greenhorn

Joined: Apr 22, 2001
Posts: 6
Hi Igor,
Are you sure of this. Are string literals not string objects, i don't know want to be clarified.
william tham
Greenhorn

Joined: Aug 10, 2001
Posts: 6
string object is deferent with literal, string object are object that create and stay in heap memory, and string literal is stay in base memory. example :
class MyClass3
{
public static void main(String asd [])
{
String a = "a";
String b = "a";
String c = new String("a");
System.out.println(a==b);
System.out.println(a==c);
System.out.println(a==c.intern());
}
}
output :
true
false
true
that mean :
1.System.out.println(a==b);
// is reference to the same value "a"

2.System.out.println(a==c);
// a not reference to the literal "a"
// and c reference to the object string in heap memory
3.System.out.println(a==c.intern());
// c.intern() is try to use String literal, if there is a "a" in the literal "pool"
in this example c.intern() get a "a" in literal, so doesn't need to create an object String in heap memory
so a and c are reference to the same literal
Siobhan Murphy
Ranch Hand

Joined: Oct 19, 2000
Posts: 72
I would have said that 2 objects are created for the same reason that Samith gives. To quote from Mughal and Rasmussen:

The easiest way of creating and initializing a String object is based on string literals:
String str1 = "You cannot touch me!";


As far as I know when you write
String s = new String("Hello");
you create 2 objects. The first object is created when the compiler encounters the literal "Hello" and the second when it encounters the 'new String'.
Shankar Ganesh
Ranch Hand

Joined: Oct 01, 2000
Posts: 34
Umm... Came across a lot of threads on String Literals & objects...
Here's what JLS ( http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101083 ) says -- hope it helps in clearing the confusion over the String Literals:

[I]
3.10.5 String Literals
A string literal is always of type String. A string literal always refers to the same instance of class String.
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.
Thus, the test program consisting of the compilation unit (�7.3):

and the compilation unit:

produces the output:

This example illustrates six points:

  • Literal strings within the same class (�8) in the same package (�7) represent references to the same String object (�4.3.1).
  • Literal strings within different classes in the same package represent references to the same String object.
  • Literal strings within different classes in different packages likewise represent references to the same String object.
  • Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
  • Strings computed at run time are newly created and therefore distinct.
  • The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
  • [/I]

- Shankar.
[This message has been edited by Shankar Ganesh (edited August 10, 2001).]
Bill Tripper
Greenhorn

Joined: May 30, 2001
Posts: 24
Originally posted by Kaushik Badiyani:
String s1,s2,s3,s4;
s1="abc";
s2=s1;
s3=s2+"zzz";
s4=s3;
How many String objects are created ??
I think it is 2.

I think the answer is 3.
Clearly there is a String object (referenced by s3) that contains a String value of "abczzz". It obviously isn't a String literal. In addition, there are the two String literals. That makes a total of three String objects.
Mateen Nasir
Ranch Hand

Joined: Aug 01, 2001
Posts: 33
yes 3 objects r created not 2
note that s3 is formed by combination 0f 2 strings "ZZZ" is a string 2
so a total of 3 objects
i guess i am correct
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Hi Bill & Matin, I also think it's 3.
But according to Shankar's specification if we change the code slightly to code:

s1="abc";
s2=s1;
s3=new String(s2+"zzz");
s4=s3;

then the total object created should be 4, right?...

------------------
azaman


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
i think the confusion lies with the word created

in the code snippet s1,s2,s4 refer to one String object which represents the String literals and i think that as far as creation of a new String object goes there will be only s3 is the one reference to a new String object that will be created with the statement s2+"zzz"


so as i said before i there will be 2 objects in this scenario
Samith.P.Nambiar
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi guys,
String literals are String objects; they are just handled differently in memory.
Ashik got it right, the example will create 4 string objects; two in the String pool, two in the heap.
s1 --> string literal "abc" in the string pool
s3 = s2 + "zzz" results in the following going on behind the scenes:

  • variable is evaluated as s2.toString() which produces a new String "abc" in heap memory this will actually return the original object
  • "zzz" is a literal so it gets created in the String pool
  • "abc" + "zzz" becomes a new string object "abczzz" in the heap


  • Result:
    2 objects "abc" and "zzz" in the String pool
    2 objects "abc" and "abczzz" in the heap
    Hope that helps.
    should be 3 objects. See later post
    ------------------
    Jane Griscti
    Sun Certified Programmer for the Java� 2 Platform
    [This message has been edited by Jane Griscti (edited August 14, 2001).]
    [This message has been edited by Jane Griscti (edited August 16, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Jane the dearess,
What an explanation! I exactly meant it but could not get the right words.... Would u have beer with me and tell me what is the number of pages in ur dictionary.
------------------
azaman
sriram gupta
Ranch Hand

Joined: Aug 09, 2001
Posts: 39
hi jane
i am a bit confused....
what i understand by ur explaination is
String sb="ev";
then if we apply toString method on this there should be two different objects one in heap and other one in the pool...
but when i apply == operator it comes out to be true...
can u explain this...
thanks
sriram gupta
Ranch Hand

Joined: Aug 09, 2001
Posts: 39
i feel there will be three objects only...
only one "abc" in the pool not in the heap
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
I think that 3 objetcs r created .The two literals & when you concatenate the two literals a third object is created.If the literals r not present in the literal pool , 2 objects will be created for these. I think so, do let me know.
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147

Result:
2 objects "abc" and "zzz" in the String pool
2 objects "abc" and "abczzz" in the heap

I have a doubt here Jane .... i think "abc" and "zzz" are both represented by the same String object in the String pool .... so would that not sum upto 3 objects ???
Samith.P.Nambiar
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Ouch ... you are right, 3 objects ... and sriram caught the flaw in my logic
s2.toString() will return the original String literal "abc" and not create a new one.
result:
2 in the string pool "abc" and "zzz"
1 in the heap "abczzz"
My apologies for the confustion.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Objects
 
Similar Threads
String puzzle
String Objects
question...
please help me
Questions from mock exam