Author: Martha Sandoval
-
Suppressed Exceptions – Exception Handling
Suppressed Exceptions The program output from GizmoTest in Example 7.18 shows that the Arithmetic-Exception was thrown in the compute() method called in the try block at (4) before the IllegalArgumentException was thrown by the implicit call to the close() method. Since only one exception can be propagated, the IllegalArgumentException thrown last would mask the ArithmeticException…
-
String Constructors – Selected API Classes
String Constructors The String class has numerous constructors to create and initialize String objects based on various types of arguments. Here we present a few selected constructors: String() Creates a new String object, whose content is the empty string, “”. String(String str) Creates a new String object, whose contents are the same as those of…
-
Numeric Wrapper Classes – Selected API Classes
Numeric Wrapper Classes The numeric wrapper classes Byte, Short, Integer, Long, Float, and Double are all subclasses of the abstract class Number (Figure 8.1, p. 425). Each numeric wrapper class defines an assortment of constants, including the minimum and maximum values of the corresponding primitive data type: Click here to view code image NumericWrapperType.MIN_VALUENumericWrapperType.MAX_VALUE The…
-
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…
-
Extracting Substrings from Strings – Selected API Classes
Extracting Substrings from Strings The String class provides methods to trim and strip strings, and also extract substrings. boolean isBlank() Returns true if the string is empty or contains only whitespace; otherwise, it returns false. See also the method isEmpty() in the CharSequence interface (p. 444). String strip()String stripLeading()String stripTrailing() Return a string whose value…
-
The Character Class – Selected API Classes
The Character Class The Character class defines a myriad of constants, including the following, which represent the minimum and the maximum values of the char type (§2.2, p. 42): Click here to view code image Character.MIN_VALUECharacter.MAX_VALUE The Character class also defines a plethora of static methods for handling various attributes of a character, and case…
-
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…
-
Converting Strings to Wrapper Objects – Selected API Classes
Converting Strings to Wrapper Objects Each wrapper class (except Character) defines the static method valueOf(String str) that returns the wrapper object corresponding to the primitive value represented by the String object passed as an argument ((2) in Figure 8.2). This method for the numeric wrapper types also throws a NumberFormatException if the String parameter is…
-
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”…
-
Converting Primitive Values and Objects to Strings – Selected API Classes
Converting Primitive Values and Objects to Strings The String class overrides the toString() method in the Object class and returns the String object itself: Click here to view code image String toString() From the CharSequenceinterface (p. 444). The String class also defines a set of static overloaded valueOf() methods to convert objects and primitive values into strings:…