• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Strings doubt

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,

Can anyone explain why this produces an output of "not Equal" while
instead of if(" String ".trim() == "String") , we replace if(" String" == "String") we get an output of "equal"... what happens internally?





Thanks in advance
[ September 04, 2007: Message edited by: Radha Kamesh ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Both of these wonot work.
to conmpare string use equals method.

hope this helps
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Radha Kamesh


In the first string have empty space, Both the strings are not equal.

Try to yourself , Remove the empty space before and after of the string.

It shows the equal otherwise , shows the UnEqual.

Here check with content of the Object.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
== operator checks if both object references refer to the same memory location eg: if (a==b) it means it verifies whether both a and b refer to the same location in memory.

equals() if for content comparison.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI........
The functionality of "==" operator and equals() method is different.

1)."==":returns true if both the references are pointing to the same object.
2).equals():returns true if both the references point to objects that are equal in their context(Value).

So in your question trim() method is creating another object and is being applied to "==" operator which will return false.

when you apply equals() method it will return true.

So follows the output.
..............................................................
Regards,
Mack
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output is:


Can anyone explain this?
3x...
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prints: Equal

String objects str3 and str4 are both created using string literals thus created in the String literal pool, and two objects created using the same literal will refer to the same object in the pool.
 
Do Re Mi Fa So La Tiny Ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic