• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

string how it works

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class AA{
public static void main(String arg[]){
if("String".replace('t','t') == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
here I want to ask whether a new string is made or the same is used when comparing.THere are lots of other method also like trim,tostring whether they work in same way.String are immutable so a new would be prepared but it aslo check whether it is avaliable in memory or not.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think with all these methods the original String is returned (so there is no new String created).
The javadoc for the function toLowerCase() says:
If no character in the string has a different lowercase version, based on calling the toLowerCase method defined by Character, then the original string is returned.
I have tried other String methods however, that dont have this �omment in javadoc, and if no changes are made, a == comparison between the original String and the resulting Stirng always returns true.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Strings are immutable and the JVM preserves reusability
Similar strings are always kept in the pool
But if its a StringBuffer then its a whole different issue
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Payal,
Please note that all the String methods like toUpperCase(),substring(),concat(),etc. would produce a new String if and only if the operation results into a new String literal.
In your example, "String".replace('t','t') will not produce a new String literal and hence it would be pointing to the same reference as "String".Thus you would get "Equal" as output.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic