Category: The CharSequence Interface
-
Chaining Exceptions – Exception Handling
Chaining Exceptions It is common that the handling of an exception leads to the throwing of another exception. In fact, the first exception is the cause of the second exception being thrown. Knowing the cause of an exception can be useful, for example, when debugging the application. The Java API provides a mechanism for chaining…
-
Using Escape Sequences in Text Blocks – Selected API Classes
Using Escape Sequences in Text Blocks We do not need to end each text line with the \n escape sequence, as the text block retains the line terminators entered directly into the text. However, using the \n escape sequence will be interpreted as literally inserting a newline character in the text. The query sql2 below…
-
The Wrapper Classes – Selected API Classes
8.3 The Wrapper Classes Wrapper classes were introduced with the discussion of the primitive data types (Table 2.16, p. 43), and also in connection with boxing and unboxing of primitive values (§2.3, p. 45). Primitive values in Java are not objects. To manipulate these values as objects, the java.lang package provides a wrapper class for…
-
The String Class – Selected API Classes
8.4 The String Class Handling character sequences is supported primarily by the String and String-Builder classes. This section discusses the String class that provides support for creating, initializing, and manipulating immutable character strings. The next section discusses support for mutable strings provided by the StringBuilder class (p. 464). Internal Representation of Strings The following character…
-
The Object Class – Selected API Classes
8.2 The Object Class All classes extend the Object class, either directly or indirectly. A class declaration, without the extends clause, implicitly extends the Object class (§5.1, p. 191). Thus the Object class is always at the root of any inheritance hierarchy. The Object class defines the basic functionality that all objects exhibit and all…
-
Converting Integer Values to Strings in Different Notations – Selected API Classes
Converting Integer Values to Strings in Different Notations The wrapper classes Integer and Long provide static methods for converting integers to text representations in decimal, binary, octal, and hexadecimal notation. Some of these methods from the Integer class are listed here, but analogous methods are also defined in the Long class. Example 8.2 demonstrates the…
-
Converting Primitive Values to Strings – Selected API Classes
Converting Primitive Values to Strings Each wrapper class defines a static method toString(type v) that returns the string corresponding to the primitive value of type, which is passed as an argument ((6a) in Figure 8.2). Click here to view code image static String toString(type v) Click here to view code image String charStr2 = Character.toString(‘\n’); //…
-
Comparing Strings – Selected API Classes
Comparing Strings Characters are compared based on their Unicode values. Click here to view code image boolean test = ‘a’ < ‘b’; // true since 0x61 < 0x62 Two strings are compared lexicographically, as in a dictionary or telephone directory, by successively comparing their corresponding characters at each position in the two strings, starting with…
-
Searching for Characters and Substrings in Strings – Selected API Classes
Searching for Characters and Substrings in Strings The following overloaded methods can be used to find the index of a character or the start index of a substring in a string. These methods search forward toward the end of the string. In other words, the index of the first occurrence of the character or substring…
-
Joining of CharSequence Objects – Selected API Classes
Joining of CharSequence Objects One operation commonly performed on a sequence of strings is to format them so that each string is separated from the next one by a delimiter. For example, given the following sequence of strings: “2014”“January”“11” we wish to format them so that individual strings are separated by the delimiter “/”: “2014/January/11”…