|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings, string buffers, and string builders is thelengthmethod, which returns the number of characters contained in the object. After the following two lines of code have been executed,lenequals 17:In addition toString palindrome = "Dot saw I was Tod"; int len = palindrome.length();length, theStringBufferandStringBuilderclasses have a method calledcapacity, which returns the amount of space allocated rather than the amount of space used. For example, the capacity of the string builder referred to bydestin theStringsDemoprogram never changes, although its length increases by 1 for each iteration of the loop. The following figure shows the capacity and the length ofdestafter nine characters have been appended to it.A string buffer or string builder's length is the number of characters it contains; its capacity is the number of character spaces that have been allocated. The
Stringclass doesn't have acapacitymethod, because a string cannot change.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.