Thanks,
Rinav Gangar
Java Consultant
***** If at first you don't succeed; call it version 1.0 *****
Andreas Svenkson wrote:There is quite alot of background to understanding this since Strings are very special in Java, being immutable for one thing and in some circumstances stored in the String literal pool for another.
Short asnswer: String.trim() returns a new String object, meaning it is not be the same object as any other String object you have created up until that point, which is why you get a false result.
// Andreas
Ikpefua wrote:
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Andreas Svenkson wrote:I do believe the String literal pool is part of the heap though? Please correct me if I'm wrong, I'd appreciate some confirmation on this just to satisfy my curiousity.
// Andreas
Thanks,
Rinav Gangar
Java Consultant
***** If at first you don't succeed; call it version 1.0 *****
Andreas Svenkson wrote:I do believe the String literal pool is part of the heap though? Please correct me if I'm wrong, I'd appreciate some confirmation on this just to satisfy my curiousity.
// Andreas
Ikpefua wrote:
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Andreas Svenkson wrote:Ikpefua.
I think of the String literal pool, as a "box inside a box". The outer box is the heap, and the inner box being the string literal pool, which is then part of the heap. Its purpose would still be the same, like you said, to prevent duplicate string objects.
I quote: "If you use the new keyword, a new String object will be created. Note that objects are always on the heap - the string pool is not a separate memory area that is separate from the heap."
This is from: http://forfivenines.blogspot.com/2009/12/javas-string-pooling.html
But just because it is written on the net doesn't mean it's true
// Andreas
Ikpefua wrote:
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Prepare for 'OCA Java SE 8 certification' with eJavaGuru.com
Author of Manning's OCA Java SE 8 bookProgrammer I Certification Guide, OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
Mala Gupta wrote:Apart from discussing the pool of Strings, an important point was left out in this discussion: Methods like substring, trim, toLowerCase etc. defined in class String return a reference to the same object, if the returned value is exactly the same. And this applies to both type of Strings - the ones that are created in the String pool and the ones that are created using the new keyword (which aren't placed in the pool of Strings).
cheers
Mala
Ikpefua wrote:
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Prepare for 'OCA Java SE 8 certification' with eJavaGuru.com
Author of Manning's OCA Java SE 8 bookProgrammer I Certification Guide, OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
Mala Gupta wrote:Ikpefua,
I always recommend finding out 'why' a method behaves in a certain manner. It goes a long way - it will help you to retain the info longer (very imp for certification preparation) and will also help you to deal with similar issues at workplace.
Mala
Ikpefua wrote:
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Prepare for 'OCA Java SE 8 certification' with eJavaGuru.com
Author of Manning's OCA Java SE 8 bookProgrammer I Certification Guide, OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
Don't get me started about those stupid light bulbs. |