9.2. Superclasses and Subclasses

 
[Page 291 ( continued )]

Chapter Summary

  • Strings are objects encapsulated in the String class. A string can be constructed using one of the eleven constructors or using a string literal shorthand initializer.

  • A String object is immutable; its contents cannot be changed. To improve efficiency and save memory, the JVM stores string literals in a unique object if two literal strings have the same character sequence. This unique object is called an interned string object.


    [Page 292]
  • You can get the length of a string by invoking its length() method, and retrieve a character at the specified index in the string using the charAt(index) method.

  • You can use the concat method to concatenate two strings, or the plus ( + ) sign to concatenate two or more strings.

  • You can use the substring method to obtain a substring from the string.

  • You can use the equals and compareTo methods to compare strings. The equals method returns true if two strings are equal, and false if they are not equal. The compareTo method returns , a positive integer, or a negative integer, depending on whether one string is equal to, greater than, or less than the other string.

  • The Character class is a wrapper class for a single character. The Character class provides useful static methods to determine whether a character is a letter ( isLetter (char) ), a digit ( isDigit(char) ), uppercase ( isUpperCase(char) ), or lowercase ( isLowerCase(char) ).

  • The StringBuilder / StringBuffer class can be used to replace the String class. The String object is immutable, but you can add, insert, or append new contents into a StringBuilder / StringBuffer object. Use String if the string contents do not require any change, and use StringBuilder / StringBuffer if they change.

  • You can pass strings to the main method from the command line. Strings passed to the main program are stored in args , which is an array of strings. The first string is represented by args[0] , and args.length is the number of strings passed.

  • The File class is used to obtain file properties and manipulate files. It does not contain the methods for reading/writing data from/to a file.

  • You can use Scanner to read string and primitive data value from a text file and use PrintWriter to write data to a text file.

  • You can use the regular expressions to represent patterns for matching, replacing, and splitting strings.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net