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
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?
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).
“The strongest of all warriors are these two — Time and Patience.” ― Leo Tolstoy, War and Peace
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.
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.
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.
posted 1 year ago
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.
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.
posted 1 year ago
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.
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!