# JDC Tech Tips Vol. 2 No. 6

JDCTechTips@sun.com
Thu, 14 Jan 1999 05:14:21 GMT

- Messages sorted by: [ date ][ thread ][ subject ][ author ]
- Next message: JDCTechTips@sun.com: "JSECURITY Issue: Registering the forms applet signature"
- Previous message: Charlie Lai: "Re: When does an Applet display a warning dialogue?"

WELCOME to the Java Developer Connection(sm) Tech Tips, Vol.2 No.6. This
issue covers StringBuffer editing; how to find bootstrap classes; and an
interesting angle on comparing strings.

J D C T E C H T I P S

TIPS, TECHNIQUES, AND SAMPLE CODE
* StringBuffer Editing
* How Bootstrap Classes are Found
* Interning Strings

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

T I P S , T E C H N I Q U E S , A N D S A M P L E C O D E

STRINGBUFFER EDITING

The Java(tm) language has two classes for representing strings: String and
StringBuffer. String represents an immutable (unchangeable) sequence of
characters (see Tech Tips Vol.1 No. 6, January 1998), while StringBuffer
is used to represent mutable character sequences (that is, they can be
modified after creation of the StringBuffer object).

The Java 2 platform adds several new methods in support of StringBuffer
editing. This example illustrates several of these methods:

public class sbedit {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("testing");
sb.replace(0, 4, "TEST");
System.out.println("after replace = " + sb);
sb.delete(5, 7);
System.out.println("after delete = " + sb);
String s = sb.substring(0, 4);
System.out.println("after substring = " + s);
}
}

The output of the program is:

after replace = TESTing
after delete = TESTi
after substring = TEST

Note that the lower index of a character range is inclusive, while the
higher one is exclusive, so for example, specifying "0,4" for a replace
means that the inclusive range 0-3 is replaced.

StringBuffer is a useful class if you're trying to do any type of string
editing, for example, in support of a text editor or word processor.

HOW BOOTSTRAP CLASSES ARE FOUND

If you've used the Java language very much over the last few years, you are
probably familiar with the idea that the location of the standard Java core
classes must be specified via the CLASSPATH environment variable. The
location would be specified by giving the path of the "classes.zip", or
more recently, the "rt.jar" file containing all the .class files for the
various Java core classes.

This area has changed recently, with the release of the Java 2 platform.
The Java Launcher (java.exe) now finds these standard classes (known as
bootstrap classes) automatically, using configuration information added to
the local system when the Java 2 platform is installed.

On rare occasions, you may need to override the standard setting, which
can be done by saying:

$ java -Xbootclasspath:/somedirectory/rt.jar

You still need to specify locations of user classes, as in earlier versions
of Java. Note also that the pathname specification of particular
directories and jar files may vary slightly between platforms, just as it
does with CLASSPATH.

INTERNING STRINGS

In the Java language there are two ways to check whether strings are
equivalent. One way is to say:

string1 == string2

and the other is:

string1.equals(string2)

The result of the == operator is true if both operands refer to the same
String object, whereas equals is used to determine whether the strings have
the same characters. In general, two strings that have the same characters
will not compare equal using the == operator, but will do so using equals.

But there's an interesting angle on this subject, which relates to
"interning" of strings. The String class has an intern method, which is
used to set up pools of strings. If I have a string s and I say:

s = s.intern();

then the contents of s are compared against an internal pool of unique
strings, and added if this particular string's contents are not already
in the pool. A reference to the unique pool String is returned.

Strings that have been interned can be compared to each other using ==,
because there's a unique string object for any given sequence of
characters representing one of the strings.

String literals and string-valued constant expressions are always
interned, so that for example:

"abc" + 37 == "abc" + 37

is always true.

Here is an example that illustrates how equality checking and interning
work:

public class intern {
public static void main(String args[])
{
String a = "abc";
String b = "abc";
if (a == b)
System.out.println("== #1"); // true
if (a.equals(b))
System.out.println("equal #1"); // true
a = "abcd";
b = "abc";
b += "d";
if (a == b)
System.out.println("== #2"); // false
if (a.equals(b))
System.out.println("equal #2"); // true
a = a.intern();
b = b.intern();
if (a == b)
System.out.println("== #3"); // true
if (a.equals(b))
System.out.println("equal #3"); // true
}
}

Interning has some initial cost to set up, but once done, supports very
efficient equality checking between strings, because it's comparing pooled
objects instead of character sequences. Interning offers a performance
advantage in the situation where the same strings are used repeatedly.

. . . . . . . . . . . . . . . . . . . . . . . .
-- NOTE
The names on the JDC mailing list are used for internal Sun
Microsystems(tm) purposes only. To remove your name from the list,
see Subscribe/Unsubscribe below.

-- FEEDBACK
Comments? Send your feedback on the JDC Tech Tips to:

JDCTechTips@Sun.com

-- SUBSCRIBE/UNSUBSCRIBE
The JDC Tech Tips are sent to you because you elected to subscribe
when you registered as a JDC member. To unsubscribe from JDC Email,
go to the following address and enter the email address you wish to
remove from the mailing list:

http://developer.java.sun.com/unsubscribe.html

To become a JDC member and subscribe to this newsletter go to:
http://java.sun.com/jdc/
-- ARCHIVES
You'll find the JDC Tech Tips archives at:

http://developer.java.sun.com/developer/javaInDepth/TechTips/index.html

-- COPYRIGHT
Copyright 1999 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.

This document is protected by copyright. For more information, see:

http://developer.java.sun.com/developer/copyright.html

The JDC Tech Tips are written by Glen McCluskey.

JDC Tech Tips Vol. 2 No. 6
January 12, 1999

- Next message: JDCTechTips@sun.com: "JSECURITY Issue: Registering the forms applet signature"
- Previous message: Charlie Lai: "Re: When does an Applet display a warning dialogue?"