• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

String (Java)

 
Greenhorn
Posts: 3
MyEclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String lol = "lol";
 System.out.println(lol.toUpperCase() == lol);    //  outputs false
 System.out.println(lol.toUpperCase() == lol.toUpperCase());    // outputs false

can anyone please tell me why these both output false
 
Saloon Keeper
Posts: 12488
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the == operator checks that the two operands refer to the same object and not that the two objects represent the same value. toUpperCase() returns a new object every time, so the == will return false, even if the two different objects both represent the string "LOL".

Instead of using the == operator, use the equals() method.
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic