• 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

Q on substring() & replace() method

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Example 1:

Example 2:

The example 1 and 2 give an output 'Equal'.


For substirng(int beginIndex) method:
The result is a newly created String object that represents a subsequence of the character sequence represented by this String object; this subsequence begins with the character at position
beginIndex and extends to the end of the character sequence.
For replace(char oldChar, char newChar) method:
If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is createdthat represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.


My qery is when the javadoc says any modification to the string because of substring() & replace() method returns a new String object, how can the //Line 1 in the above two examples print "Equals" as we are comparing the references of two diff object.
I am confused .
Plz help
Reshma
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An important thing to know is that if the modified string contains the exact same content as the original string, then the original string is returned and no new string object is created.
It helps to go see what happens under the hood. Here is the code for the substring method:

As you can see, substring returns this if beginIndex is 0 and endIndex is the index of the last character of the string.
As for the replace method:

you can easily see that if the old and the new char are the same, this is returned.
I agree it would help it the doc would mention this information.
[ March 21, 2003: Message edited by: Valentin Crettaz ]
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot valentin, your explanation was great.

Reshma
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic