Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Technical Tips

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 

Tech Tips
January 14, 1999

This issue presents tips, techniques, and sample code for the following topics:

StringBuffer Editing

The JavaTM language has two classes for representing strings: String and StringBuffer. String represents an immutable (unchangeable) sequence of characters (see Tech Tips, January 20, 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.

chiclets

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 typing:

$ 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 <ACRONYM TITLE="Java ARchive">JAR</ACRONYM> files may vary slightly between platforms, just as it does with <VAR>CLASSPATH</VAR>.

Interning Strings

In the Java language there are two ways to check whether strings are equivalent. One way is to write:
  
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 the equals method.

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 the == operator, 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 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.

coffeecup


Print Button
[ This page was updated: 21-Sep-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary | Feedback | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.