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 thelength
method, which returns the number of characters contained in the object. After the following two lines of code have been executed,len
equals 17:In addition toString palindrome = "Dot saw I was Tod"; int len = palindrome.length();length
, theStringBuffer
andStringBuilder
classes 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 bydest
in theStringsDemo
program never changes, although its length increases by 1 for each iteration of the loop. The following figure shows the capacity and the length ofdest
after 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
String
class doesn't have acapacity
method, 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.