This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

StringBuilder.charAt()

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, a bit foolish question may-be, but: There are a few functions which I have seen in the SCJP book from Kathy Sierra as String member functions I should know for the exam, which I think are also StringBuilder functions.

charAt
length
substring

Are these also StringBuilder functions? And are there questions with possible answers like 'this does not compile since StringBuilder does not have a member called substring'. Since I would, reading the book, have voted 'yes this is no StringBuilder function.

I've looked for the member of StringBuilder/StringBuffer in:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html
 
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
Unrelated classes can certainly have members (methods and fields) with the same names.

Indeed, String and StringBuilder (and StringBuffer) are examples of classes that have methods sharing the same names. But it's important to recognize that these classes are not related (none are subtypes of the other), and although the methods have the same names and perform similar functions, there are important differences.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Marc said, they are totally separate classes.

However, note that they have exactly the same methods - so you can do all the same things with them. The big difference is that StringBuffer is threadsafe, therefore StringBuilder is probably faster.

The above is mentioed several times in the book!!! So be sure that you are taking in every sentence. Yes, there may be questions that check your knowledge of these API classes and their methods, so for classes such as these be sure to study the API JavaDocs.

Good luck.
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heej, Martin S J, I think I have not explained myself clearly enough in my question: I am talking about a few methods that are the same in the String class and in the StringBuilder and/or the StringBuffer class.

I know StringBuilder and StringBuffer have the same functions, but that is not my question. I can see 3 function mentioned in the book as methods of the class String, but I can see in online documentation that for example charAt is not only a method of the class String but also a method of both the classes StringBuilder and StringBuffer.

And hence my question: should I know that? Since form reading the book only, I would have thought that the 'String class' methods would not exist in StringBuilder/StringBuffer.
 
S J Martin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Ooops, I see on re-reading that I miss-read your question, sorry.

When I did SCJP I did not set out to memorize vast lumps of the Java API (my memory is not particularly good - I know what I know by /understanding/ not by having a photographic memory of 10,000 API pages ;-) . Thus, I made sure that I knew the responsibilities of such classes (String, the relevant bits of java.io, etc, etc). Be sure to have a play with the classes (especially the IO, Matcher, and use of Generics, do *lots* of experimenting). Then I felt confident that I'd be able to answer questions from what I knew to be reasonable.

If it's any use I got 93% (with my poor memory). I was disappointed to have dropped 6 questions (sorry, but I'm hard on myself). My impression is that if you've the understanding, you don't need the memory.

HTH.
 
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 Marc Wentink:
...I would have thought that the 'String class' methods would not exist in StringBuilder/StringBuffer.


As I mentioned above, there is no reason that unrelated classes can't have members with the same names. In other words, having a certain method in one class is no reason to think that a different class wouldn't have a method by that same name.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic