• 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, StringBuilder, etc

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a C++ developer , I am doing my nest to understand about Strings, and I have some questions...

1. Why would the method substring(int begin, int end) of the class String has the first parameter be 0-based index and the second parameters be 1-based index???

2. Why/when would you use String over StringBuilder, and vice versa?

3. I understand that the class String will create abandoned objects, but won't the Garbage Collector handle that in the background?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) I don't know. I'm sure there was a reason the API designers went that route.
2) You use StringBuilder when appending to a string multiple times.
3) Yes. But this wastes resources. If I am concatenating large strings very quickly, I'm using a ton of memory. Holding everything up to let the garbage collector run can greatly slow things down.
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rachel Glenn wrote:As a C++ developer , I am doing my nest to understand about Strings, and I have some questions...
1. Why would the method substring(int begin, int end) of the class String has the first parameter be 0-based index and the second parameters be 1-based index???



I think the reason this was done is because you get the length of the substring by subtracting the 1st index from the second. Here's the speel from the Ofiicial String docs:

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

 
reply
    Bookmark Topic Watch Topic
  • New Topic