SCJP 6,SCWCD 5,SCBCD 5
Failure is not an option.
SCJP6 - 93% SCWCD5 - 97%
Woohoo passed SCJP 1.6, that's the theory exam passed now for the practice ;)
SCJP 6,SCWCD 5,SCBCD 5
Failure is not an option.
J Westland wrote:You then make the substring of long, by saying String sub = long.Substring(start, end); sub is then made by just referring to this really big String. So, we still just have one big String and one substring pointing to a part of it. This is the way Strings can save memory no need to keep making Strings for words you already have.
J Westland wrote:However...if you says String sub = new String(long.Substring(start, end)); you will get a totally new string that is independent of the String called long that is in the String pool. So now there will be two objects in the String pool, the really long String long, the String sub and on the heap we also have sub as an Object.
Siva Masilamani wrote:
My understanding is, if i create a string using new operator java checks the constant pool for the literal having the same content and creates a new Object in the heap and copy the content to this new object.But if there is no such literal present in the constant pool then it creates new object in the heap and also places a reference to that literal in the pool.
Woohoo passed SCJP 1.6, that's the theory exam passed now for the practice ;)
When you call the new operator, java will instantiate an object for you, which you can assign to a reference. It should have absolutely no affect on the string pool. This behavior is no different when instantiating any other class type with the new operator.
SCJP 6,SCWCD 5,SCBCD 5
Failure is not an option.
if a string is created using new operator then actually two objects are created and then she stated that java will create a new object in the normal pool memory and make a reference to user then places the literal in the pool?
But what i read is even string objects are created in the heap and only the reference are stored in the constat pool?
SCJP 6,SCWCD 5,SCBCD 5
Failure is not an option.
Henry Wong wrote:
J Westland wrote:You then make the substring of long, by saying String sub = long.Substring(start, end); sub is then made by just referring to this really big String. So, we still just have one big String and one substring pointing to a part of it. This is the way Strings can save memory no need to keep making Strings for words you already have.
This is not true. Unless, the start and end are zero, and the actual end of the very large string respectively, you will get a new string back. There is no reference to the same space within the very large string.
Henry
Henry Wong wrote:
J Westland wrote:However...if you says String sub = new String(long.Substring(start, end)); you will get a totally new string that is independent of the String called long that is in the String pool. So now there will be two objects in the String pool, the really long String long, the String sub and on the heap we also have sub as an Object.
This is not true. As before, unless start and end represent the orignal string back, you will get a new string. This new string is passed to the constructor of the string, and yet another string is created. This second new string is then assigned to the sub reference. Furthermore, since no other reference now points to the response from the substring call, it is eligible for garbage collection. And finally, this operation has no affect on the string pool.
Henry
Actually, while the terminology Jawine used might not be 100% exact, the gist of what she says is true. As a speed optimisation, the java.lang.String.substring implementation shares the underlying char buffer, and simply stores an offset and length into the substring.
Substring returns itself if it is passed 0 for beginIndex and length() for endIndex.
For any other valid values it will return a new String object, which shares the underlying char[] (which is where most of the memory is)
Henry Wong Wrote:
As before, unless start and end represent the orignal string back, you will get a new string. This new string is passed to the constructor of the string, and yet another string is created. This second new string is then assigned to the sub reference. Furthermore, since no other reference now points to the response from the substring call, it is eligible for garbage collection. And finally, this operation has no affect on the string pool.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |