Thursday, December 25, 2008

Work with Strings

Working with Strings

Strings are one of the most common types of objects in Java. Throughout this book are various techniques for working with strings. You've seen how to create string variables, how to concatenate strings, and how to compare strings. But so far, I've only scratched the surface of what you can do with strings. In this chapter, I dive deeper into what Java can do with string. (Hint: Way more than Cat's Cradle.)

I start with a brief review of what I covered so far about strings, so you don't have to go flipping back through the book to find basic information. Then I look at the String class itself and some of the methods it provides for working with strings. Finally, you examine two almost identical classes named StringBuilder and StringBuffer that offer features not found in the basic String class.

Reviewing Strings

To save you the hassle of flipping back through this book, the following paragraphs summarize what is presented in earlier chapters about strings:

  • Strings are reference types, not value types, such as int or boolean.

    As a result, a string variable holds a reference to an object created from the String class, not the value of the string itself.

  • Even though strings aren't primitive types, the Java compiler has some features designed to let you work with strings almost as if they were. For example, Java lets you assign string literals to string variables, like this:

         String line1 = "Oh what a beautiful morning!";

  • Strings can include escape sequences that consist of a slash followed by another character. The most common escape sequences are \n for new line and \t for tab. If you want to include a slash in a string, you must use the escape sequence \\.

  • Strings and characters are different. String literals are marked by quotation marks; character literals are marked by apostrophes. Thus “a” is a string literal that happens to be one character long. In contrast, ‘a’ is a character literal.

  • You can combine, or concatenate, strings by using the + operator, like this:

         String line2 = line1 + "\nOh what a beautiful day!";
  • You can also use the += operator with strings, like this:

         line2 += = "\nI've got a beautiful feeling";
  • When used in a concatenation expression, Java automatically converts primitive types to strings. Thus Java allows the following:

         int empCount = 50;
    String msg = "Number of employees: " + empCount;

  • The various primitive wrapper classes (such as integer and double) have parse methods that can convert string values to numeric types. Here's an example:

         String s = "50";
    int i = Integer.parseInt(s);
  • You can't compare strings using the equality operator (==). Instead, you should use the equals method. Here's an example:

         if (lastName.equals("Lowe"))
    System.out.println("This is me!");
  • The String class also has an equalsIgnoreCase method that compares strings without considering case. Here's an example:

         if (lastName.equalsIgnoreCase("lowe"))
    System.out.println("This is me again!");

DESIGN PATTERN

No comments:

Post a Comment