This week's book giveaway is in the Raspberry Pi forum.
We're giving away four copies of Getting started with Java on the Raspberry Pi and have Frank DelPorte on-line!
See this thread for details.
Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

String Objects

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Igor,
Are you sure of this. Are string literals not string objects, i don't know want to be clarified.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ashik Uzzaman
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i feel there will be three objects only...
only one "abc" in the pool not in the heap
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic