Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Thinking in Java
Prev Contents / Index Next

The String and
StringBuffer classes

Here is an overview of the methods available for both String and StringBuffer so you can get a feel for the way they interact. These tables don’t contain every single method, but rather the ones that are important to this discussion. Methods that are overloaded are summarized in a single row.

First, the String class:

Method

Arguments, Overloading

Use

Constructor

Overloaded: default, String, StringBuffer, char arrays, byte arrays.

Creating String objects.

length( )


Number of characters in the String.

charAt( )

int Index

The char at a location in the String.

getChars( ), getBytes( )

The beginning and end from which to copy, the array to copy into, an index into the destination array.

Copy chars or bytes into an external array.

toCharArray( )


Produces a char[] containing the characters in the String.

equals( ), equals-IgnoreCase( )

A String to compare with.

An equality check on the contents of the two Strings.

compareTo( )

A String to compare with.

Result is negative, zero, or positive depending on the lexicographical ordering of the String and the argument. Uppercase and lowercase are not equal!

regionMatches( )

Offset into this String, the other String and its offset and length to compare. Overload adds “ignore case.”

boolean result indicates whether the region matches.

startsWith( )

String that it might start with. Overload adds offset into argument.

boolean result indicates whether the String starts with the argument.

endsWith( )

String that might be a suffix of this String.

boolean result indicates whether the argument is a suffix.

indexOf( ), lastIndexOf( )

Overloaded: char, char and starting index, String, String, and starting index.

Returns -1 if the argument is not found within this String, otherwise returns the index where the argument starts. lastIndexOf( ) searches backward from end.

substring( )

Overloaded: starting index, starting index, and ending index.

Returns a new String object containing the specified character set.

concat( )

The String to concatenate.

Returns a new String object containing the original String’s characters followed by the characters in the argument.

replace( )

The old character to search for, the new character to replace it with.

Returns a new String object with the replacements made. Uses the old String if no match is found.

toLowerCase( ) toUpperCase( )


Returns a new String object with the case of all letters changed. Uses the old String if no changes need to be made.

trim( )


Returns a new String object with the white space removed from each end. Uses the old String if no changes need to be made.

valueOf( )

Overloaded: Object, char[], char[] and offset and count, boolean, char, int, long, float, double.

Returns a String containing a character representation of the argument.

intern( )


Produces one and only one String reference per unique character sequence.

You can see that every String method carefully returns a new String object when it’s necessary to change the contents. Also notice that if the contents don’t need changing, the method will just return a reference to the original String. This saves storage and overhead.

Here’s the StringBuffer class:

Method

Arguments, overloading

Use

Constructor

Overloaded: default, length of buffer to create, String to create from.

Create a new StringBuffer object.

toString( )


Creates a String from this StringBuffer.

length( )


Number of characters in the StringBuffer.

capacity( )


Returns current number of spaces allocated.

ensure-
Capacity( )

Integer indicating desired capacity.

Makes the StringBuffer hold at least the desired number of spaces.

setLength( )

Integer indicating new length of character string in buffer.

Truncates or expands the previous character string. If expanding, pads with nulls.

charAt( )

Integer indicating the location of the desired element.

Returns the char at that location in the buffer.

setCharAt( )

Integer indicating the location of the desired element and the new char value for the element.

Modifies the value at that location.

getChars( )

The beginning and end from which to copy, the array to copy into, an index into the destination array.

Copy chars into an external array. There is no getBytes( ) as in String.

append( )

Overloaded: Object, String, char[], char[] with offset and length, boolean, char, int, long, float, double.

The argument is converted to a string and appended to the end of the current buffer, increasing the buffer if necessary.

insert( )

Overloaded, each with a first argument of the offset at which to start inserting: Object, String, char[], boolean, char, int, long, float, double.

The second argument is converted to a string and inserted into the current buffer beginning at the offset. The buffer is increased if necessary.

reverse( )


The order of the characters in the buffer is reversed.

The most commonly used method is append( ), which is used by the compiler when evaluating String expressions that contain the ‘+’ and ‘+=’ operators. The insert( ) method has a similar form, and both methods perform significant manipulations to the buffer instead of creating new objects.

Thinking in Java
Prev Contents / Index Next

 
 
   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire