• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"String" and == with methods?

 
Greenhorn
Posts: 2
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The Answer is Not Equal
but Why???




but if I remove the trim method and also the space before and after the " String " -----> "String"
the the output is EQUAL???


I know that the == operator when used for comparison always checks for the memory location and not for the actual content of the string

So when I declare a String on the fly i.e. locally in a if condition where does it get stored and
does that if condition creates two physical, separate memory for "String"???


my guess is on the fly both of the Strings are same and at the same memory location
but when we apply trim to it a new String is created and therefore in a different memory location
and we get NOT EQUAL

and instead if I use .equals() it gives Strings are EQUAL...


this is my understanding, correct me if I am Wrong

Thanks and Regards,
Rinav G
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:


Hello Rinav, hello Andreas...

@Rinav, in addition to your analysis and that of Andreas, I want to specifically mention that string literals, "string" live in the String pool, while string objects, new String("string") live in the heap, the 'identification' representing the string in both locations are different, logically the == will return false.
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rinav Gang
Greenhorn
Posts: 2
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



yea, I too thought of the same thing but
we say that all the Objects live in Heap, for String it's String Pool

may be I need to read more on Strings...

Thanks n Regards ,
Rinav

 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:


@Andreas...If String literal pool is a part of the heap, my curious question will be what then is the use of the pool???
Haven said that, based on what I studied in the K & B book regarding this question and to the best of my understanding, String literal pool is a specific memory system operated by the JVM. ( specific means a heap that does NOT store objects created by the new keyword) its purpose is to avoid the creation of uncountable number of duplicate strings on the heap.

Summary: String literal pool is a heap specifically to store String literals, and why I used the expression "memory system" is because the pool prevents duplicates, the JVM checks the pool for a string literal that you created in your program and if that literal already exists in the pool, it will REDIRECT your reference variable to that literal in the pool. (Funny enough, we might have to ask Oracle Or Sun what then happens to your string created in the program.)

I hope this helps.
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:


Andreas...I can see now that I misunderstood you, of course there is a lot of sense in what you are saying, and neither did I say that the String pool is SEPARATE from the heap, I said that it is NOT the heap, whether they are together or they are not together, or whether one is inside the other is okay, but here the conclusion is that we recognise this FUNDAMENTAL difference.
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
English grammar can sometimes cause unecessary misunderstandings ;)


// Andreas
 
Author
Posts: 375
22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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). The following code verifies this statement:


cheers
Mala
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:


Hello Mala, you are very correct!, I have been studying Strings today and I found out this behaviour.
 
Mala Gupta
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

You can examine the source code of String class and confirm this behaviour (just in case you have any other doubts) :-)

cheers
Mala
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:


Mala you are ABSOLUTELY correct, I already started going into details of methods a while ago, I FAILED my last exams precisely for this reason, I did poorly in Strings, IO, Formatting, Parsing. So in my current studies towards a retake I am NOT leaving any stone unturned!. Thanks a lot for the advice!.
 
Mala Gupta
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ikpefua,

Great! Good Luck to you!

cheers
Mala
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic