• 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" == "String"

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers
Here in these three codes, when i compare to real strings then it results true. but when i trim the string which has space and compare it then it gives false.
I understand that string with space will sit on the another place in string pool and it's a different object. But once we trim it then it won't comapre the same string objects?
Confuse in these fact.
System.out.println("java"=="java"); // true
System.out.println((" Java".trim()).equals("Java")); // false

System.out.println(" Lower Case = " + (("JAVA".toLowerCase())=="java")); // false
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first gives me true!
second if u change the == to equals gives me true also.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually your 2nd line
System.out.println((" Java".trim()).equals("Java"));
prints true.
my question is why
"aaaa".trim() == "aaaa" is true (1)
but
" aaaa".trim() == "aaaa" is false? (2)
i understand (2) is correct, because trim() returns a new object. (1) is intriguing.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill
From the api documentation "If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned"
So "aaaa".trim() will return a reference to "aaaa", hence comparison is true.
 
bill williams
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greame,
Thanks. I probably shd read api/jls more closely
 
shiren shah
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry bill,
i made a mistake, i wanted to ask only for ==. so just coding mistake. i understand that equals gives true value but while we trim any string object then why the trimmed string and string with == operator differs.
Both in trim example and lowercase as below gives false output.
System.out.println("Trim =" + (" Java".trim()=="Java")); // false

System.out.println(" Lower Case = " + (("JAVA".toLowerCase())=="java")); // false {
 
reply
    Bookmark Topic Watch Topic
  • New Topic