Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

String constant pool

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am preparing for the OCA Java 8 exam and I am confused in regards to strings and what
happens when they're created. I understand that Strings are created in one of two methods: by "new"
keyword and by String literals. When created by String literals, Java first looks inside the String
constant pool to see if there are any matching Strings, if non are available then a new String object is
created and a reference to it is returned. When created by the "new" keyword a new String object is
created in the non pool heap. Both String objects are not related even if both String objects have the
same characters.
I hope I have this concept correct. My question is, when creating a String object by means of the
"new" keyword, does the String object's created by the "new" keyword String literal go inside the
String constant pool and becomes available for reuse or those it permanently stay outside the pool?
I feel that because it's created in the non pool heap and because you can create Strings using both
methods and both objects won't equal to be the same but rather point to different references, Strings
created using the "new" keyword will create a non pool String and the String stays outside the pool
and cannot be use by any other reference variable?

 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the purposes of the Java OCA 8 exam:
- Any time you create a string object using "new" you are creating a new object which does not get put into the string pool.
   There may be one or two methods which can put a new string object into the string pool, but you do not need to know that for the Java OCA 8 exam.
- If you do not use the "new" keyword when creating strings then first the system looks for the string in the string pool. If found it uses that one, otherwise it creates a new one.
- There is a limit of how many strings which can be in the string pool, I think it 128 or 256. This may be configurable when compiling the Java binaries (Javac and Java).
 
Marshal
Posts: 6944
471
Mac OS X VI Editor BSD Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pete Letkeman wrote:- Any time you create a string object using "new" you are creating a new object which does not get put into the string pool.


I don't think that is entirely true.

Correct, object which is being referred by reference variable doesn't go to string constant cool, but, the string literal it contains - does.


So what you'd end up here is, that it it would create 2 objects and one reference variable. One object would sit in a heap memory and the actual literal "OCA" would be placed in the string constant pool. Reference variable "s" would refer to an object in the heap.

Somebody would correct me if I'm mistaken here.
 
Marshal
Posts: 65019
246
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . the string literal it contains - does. . . ..

It is possible to call new String(...) without a String literal: String text = new String(somethingElse.toString()); in which case there would be nothing to go in the String pool. Though we all know that sort of code is pretty daft.
 
Liutauras Vilda
Marshal
Posts: 6944
471
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Didn't think about such an option. Joshua I think can consider this for puzzlers as can be well confusing.
 
ethan crown
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the deep insight into what at first appears to be a simple concept.
So just to further clarify:


String s = new (“OCA”);

1) Creates a reference variable named (s)
pointing at a String object.

2) A new object is instanciated and memory
is allocated for the object in the general heap.

3) The String literal “OCA” is created without
being first searched for in the String constant
pool.

4) The String literal “OCA” is placed inside the
String constant pool and the  String literal value
“OCA” can be used by any other object who will
use the same String literal value “OCA”.

5) The reference variable (s) points at the  
object which has the String literal value “OCA” inside
The String constant pool.

6) Any other reference variable can reference the object with
the String literal value “OCA” in the constant String pool.....right!?


 
Liutauras Vilda
Marshal
Posts: 6944
471
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure about the underlying mechanics of 3, but 5 is incorrect. Reference variable would refer to an object in a heap, not string constant pool.

And you are welcome
 
Liutauras Vilda
Marshal
Posts: 6944
471
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

earlier, I wrote:but 5 is incorrect. Reference variable would refer to an object in a heap, not string constant pool.


You can check this hypothesis with the following code snippet:

If that is true what you were claiming, this program should print "true", otherwise "false".

Note, I'm using "==", so it checks whether string objects referring to the same location in a memory.
 
ethan crown
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK now I am confident about my knowledge of how the creating of
a String works. @Luitauras Vilda just as you first posted, when
creating Strings using the "new" keyword there are two objects
being created. The one for the named reference variable s pointing
at the object in the normal heap which does contain the value "OCA"
and the second object the String literal value which contains "OCA"
inside the String constant pool.
The String literal value "OCA" is now available for any other to use and
the reference variable (s) is pointing at its own String literal "OCA" in
the normal heap.
Indeed, two objects one reference variable.
 
Liutauras Vilda
Marshal
Posts: 6944
471
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems you got it right
 
Campbell Ritchie
Marshal
Posts: 65019
246
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ethan crown wrote:. . . the reference variable (s) is pointing at its own String literal "OCA" in the normal heap. . . .

What String literal? The variable s doesn't point to a String literal in the original code, but to a String object representing “OCA”. The String literals go into the constant pools, yes, but not in the form of literals. A literal only exists in the source code and it is another way to represent a String object; only String objects exist in memory.
 
ethan crown
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right, it was what I was referring to. As in Java
just about everything is an object! Thank you though
for further breaking this down for me.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!