The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

## Characters and Strings

> The Java platform contains three classes that you can use when working with character data:
> - Character--A class whose instances can hold a single character value. This class also defines handy methods that can manipulate or inspect single-character data.
> - String-- A class for working with immutable (unchanging) data composed of multiple characters.
> - StringBuffer--A class for storing and manipulating mutable data composed of multiple characters.

#### Characters

> An object of `Character` type contains a single character value. You use a `Character` object instead of a primitive `char` variable when an object is required-for example, when passing a character value into a method that changes the value or when placing a character value into a data structure, such as a vector, that requires objects. The following sample program, `CharacterDemo`, creates a few character objects and displays some information about them. The code that is related to the `Character` class is shown in boldface:

> >     public class CharacterDemo {
> >         public static void main(String args[]) {
> >             Character a = new Character('a');
> >             Character a2 = new Character('a');
> >             Character b = new Character('b');

> >             int difference = a.compareTo(b);

> >             if (difference == 0) {
> >                 System.out.println("a is equal to b.");
> >             } else if (difference < 0) {
> >                 System.out.println("a is less than b.");
> >             } else if (difference > 0) {
> >                 System.out.println("a is greater than b.");
> >             }
> >             System.out.println("a is "
> >                                + ((a.equals(a2)) ? "equal" : "not equal")
> >                                + " to a2.");
> >             System.out.println("The character " + a.toString() + " is "
> >                 + (Character.isUpperCase(a.charValue()) ? "upper" : "lower")
> >                 + "case.");
> >         }
> >     }

> The following is the output from this program:

> >     a is less than b.
> >     a is equal to a2.
> >     The character a is lowercase.

> The `CharacterDemo` program calls the following constructors and methods provided by the `Character` class:
> `Character(char)`
>   The `Character` class's only constructor, which creates a `Character` object containing the value provided by the argument. Once a `Character` object has been created, the value it contains cannot be changed.

> `compareTo(Character)`
>   An instance method that compares the values held by two character objects: the object on which the method is called (a in the example) and the argument to the method (b in the example). This method returns an integer indicating whether the value in the current object is greater than, equal to, or less than the value held by the argument. A letter is greater than another letter if its numeric value is greater.

> `equals(Object)`
>   An instance method that compares the value held by the current object with the value held by another. This method returns `true` if the values held by both objects are equal.

> `toString()`
>   An instance method that converts the object to a string. The resulting string is one character in length and contains the value held by the character object.

> `charValue()`
>   An instance method that returns the value held by the character object as a primitive `char` value.

> `isUpperCase(char)`
>   A class method that determines whether a primitive `char` value is uppercase. This is one of many `Character` class methods that inspect or manipulate character data.

> > Note to C and C++ Programmers: Java strings are first-class objects, unlike C and C++ strings, which are simply null-terminated arrays of 8-bit characters.

> This section covers these string-related topics:
> - Why Two String Classes?
> - Creating Strings and StringBuffers
> - Accessor Methods
> - Modifying StringBuffers
> - Converting Objects to Strings
> - Converting Strings to Numbers
> - Strings and the Java Compiler

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.