• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai ,
Anyone pls explain how many objects are created in the following code:

Also, in this code


pls explain in detail.Thanks in advance
Vineela
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
heyas!
firstly, strings are immutable. this is important for scjp. it basically means that for resource purposes, any strings you create are put in a string pool. if you try to create a string with identical chars to an existing string, the jvm will just create a new referece, but point to the existing, identical string in the pool

firstly string 's' object is created and given 'hai' as its value
sencondly it is placed in the string pool
so we have STRING POOL: 'Hai'
references: s => Hai
when we call string str ... blah we then create a new reference, called str, but it points to the existing string; Hai
so we have STRING POOL: 'Hai'
references: s => Hai
str => Hai
where => means points to
...........
>>Also, in this code

simply put, two references are created s1 and s2. in the string pool two objects are created, Hai and Hello
String Pool: 'Hai' , 'Hello'
References: s1 => Hai
s2 => Hello
therefore if i were to create String example = "Hello"; we would also have
example referenceing to an existing 'Hello' string in the pool
example => Hello
hope that helps!
-twans
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to be really picky - the execution of the statement:
String s = "hai" ;
does not actually create a new String because the String literal "hai" was compiled into the class file by the compiler.
Thats why I hate trying to write "how many objects" questions with Strings.
Bill
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Antoine,
I think you have missed one point here.
When we say,
String s = "Hai";
One string object is created in the String pool and one reference is pointing to that object.
When we say,
String s1 = new String("Hello");
Here two string objects are created, one in the string pool and one in heap.
But i don't know 's1' is pointing to which objects.
Is it not correct?
Thanks,
Narasimha.
 
Antoine Waugh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the clarification guys
-twans
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr.William
I could not get ur reply.Can u explain in detail?
1.When we say String s1="Hai"....a reference variable s1 is created and points to a value "Hai".Incase if there is no vlue "Hai" in the string pool then will compiler creates a string "Hai" in speing pool?
An object is created with new operator only.Untill and unless we create a s1 object with new String ,no instance is created.How the situation is handled here ???
 
Vineela Devi
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Narasimha,
I think what u said is correct.
In this case String s1 = new String("Hello"); s1 will be refering to the object created in the heap.
What do u say abt this?
Also, i want to have a clarification as total how many objects are created in the codes posted by me i.e. how many objects r created in code 1 and how many in code 2?
Vineela.
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vineela,
Also, i want to have a clarification as total how many objects are created in the codes posted by me i.e. how many objects r created in code 1 and how many in code 2?
[Corrected]
IMO, one object is create in the first case code 1. This String object contains "Hai". This object has two references to it s
and str.
In Code 2, two objects are created. The first one referenced by
"s1" containint the String "Hai" and the second object referenced
by "s2" containing the String "Hello".
Hope this helps.
Quoting Section 4.3.3 of JLS


4.3.3 The Class String
Instances of class String represent sequences of Unicode characters. A String object has a constant (unchanging) value. String literals (�3.10.5) are references to instances of class String.
The string concatenation operator + (�15.18.1) implicitly creates a new String object.


Although, Section 4 has a different tone....


An object (�4.3.1) is a dynamically created instance of a class type or a dynamically created array. The values of a reference type are references to objects. All objects, including arrays, support the methods of class Object (�4.3.2). String literals are represented by String objects (�4.3.3).


Some of the best discussions I had in the past are here
Discussion 1 and Discussion 2.
Enjoy............
[ February 24, 2004: Message edited by: Madhav Lakkapragada ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic