next up previous
Next: Method signature and method Up: Unit 02 Previous: Objects and classes in

The class String

Let us analyze the predefined class String. The objects that are instances of this class denote strings, i.e., sequences of characters.

Expressions delimited by double quotes, e.g., "java", denote objects of the class String (to be precise, these are predefined references to objects of the class String), and are called String literals. In general, literals represent constants, hence String literals represent constants of type string.

The class String provides various methods, including (the following table is taken from the Java documentation):

 String concat(String str)
          Concatenates the specified string to the end of this string.
 int length()
          Returns the length of this string.
 String substring(int beginIndex)
          Returns a new string that is a substring of this string.
 String substring(int beginIndex, int endIndex)
          Returns a new string that is a substring of this string.
 String toLowerCase()
          Converts all of the characters in this String to lower case using the rules of the default locale.
 String toUpperCase()
          Converts all of the characters in this String to upper case using the rules of the default locale.
 String trim()
          Returns a copy of the string, with leading and trailing whitespace omitted.

Example:

public class MyMessage3 {
  public static void main(String[] args) {
    System.out.println("java".toUpperCase());
  }
}

The method toUpperCase() of the class String converts a string to upper case letters. In this case, toUpperCase is called on the object (denoted by the literal) "java", and it returns a reference to a new object of the class String that denotes the string "JAVA", and which is printed by the program.


next up previous
Next: Method signature and method Up: Unit 02 Previous: Objects and classes in