• 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
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

String question

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code:
<code>String s = new String("test");</code>
how many objects are ctreated?
a. One object (s).
b. Two objects (s and intern string "test").
My answer is b.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s is not an object, it is an object reference. Therefore, only one object is created
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, there are two objects - neither one is s, but one is currently referenced by s - that's the one which was created by the new operator. The other wass created in the intern string pool at class loading time, unless there was already a matching string in the pool at that time. So "two" seems like the best answer.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jim,
I thought that a String is created in the String pool only IF it is not created explicitly with the 'new' operator. Can you explain? Thanks!
 
Ihor Strutynskyj
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I should be more precise with differentiation between objects and their references.
The trick is wherever in the code you use "test" or other literal it will be matched against global string pool and standard string literal handling technique applied to it.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Nut - the new String() constructor is taking another String as its parameter - where did this string come from? That's the string created from the literal "test", and it is created in the intern pool. It must exist before the new String() can be executed. So that's why there are two strings.
Also - it's not only strings created with "new" which are not interned. Rather, it's only strings created from literals and constant expressions which are interned. Check out http://www.javaranch.com/ubb/Forum24/HTML/000530.html (5th message) where I explain this in more detail.
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic