Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Strings Comparison

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt in String comparisons.
i am clear about toString(),==& equals.But still could not make up logic for the answers .


1. "SCJP"=="scjp".toUpperCase(); //FALSE ..How..
,But according to literal pool concept , "scjp".toUpperCase() result would go to literal pool
2. String s1="SCJP";
String s2="";
s1==s1+s2 //FAlSE ..How

3.String s1="SCJP";
String s2=s1.concat("")
s1==s2 ; //FAlsE ...How

Thankx
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a string is interned, then it is in the String pool:

I imagine that strings are not automatically interned to save time.

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • "scjp".toUpperCase()
  • s1 + s2
  • s1.concat("")
  • Each of these expressions result in a new String, which is not a literal, and not in the pool (unless intern is called on it). Therefore, the == comparison will return false, because these are new objects.
     
    Lucky J Verma
    Ranch Hand
    Posts: 278
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "SCJP"=="scjp".toUpperCase();
    this expression is already having a refernce to SJP in literal pool
    when toUppercase() ,produces SCJP ,isnt its reference picked from literal pool.
    result produces false, but i thought it should be true
     
    Kaydell Leavitt
    Ranch Hand
    Posts: 694
    Mac OS X Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think that my last code example wasn't exactly right. You'd want to intern the string sooner.

    I think that this code creates one less String:

     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Lucky J Verma:
    ...when toUppercase() ,produces SCJP ,isnt its reference picked from literal pool...


    No, a new "SCJP" String is created on the heap at runtime, just as if new String("SCJP") had been called.

    See Strings Literally.
     
    WARNING! Do not activate jet boots indoors or you will see a tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic