Category: Advantages of Exception Handling
-
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…
-
Formatted Strings – Selected API Classes
Formatted Strings We have used the System.out.printf() method to format values and print them to the terminal window (§1.9, p. 24). To just create the string with the formatted values, but not print the formatted result, we can use the following static method from the String class. It accepts the same arguments as the printf()…
-
Reading Lines from a String – Selected API Classes
Reading Lines from a String The following method can be used to extract lines from a string: Stream<String> lines() Returns a stream of lines extracted from this string, separated by a line terminator (§16.4, p. 902). A line is defined as a sequence of characters terminated by a line terminator. A line terminator is one…
-
Wrapper Comparison, Equality, and Hash Code – Selected API Classes
Wrapper Comparison, Equality, and Hash Code Each wrapper class implements the Comparable<Type> interface, which defines the following method: Click here to view code image int compareTo(Type obj2) This method returns a value that is less than, equal to, or greater than zero, depending on whether the primitive value in the current wrapper Type object is less…
-
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 try-with-resources Statement 2 – Exception Handling
A lot of boilerplate code is required in explicit resource management using try-catch-finally blocks, and the code can get tedious and complex, especially if there are several resources that are open and they all need to be closed explicitly. The try-with-resources statement takes the drudgery out of associating try blocks with corresponding finally blocks to…
-
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’); //…
-
The CharSequence Interface – Selected API Classes
The CharSequence Interface This interface defines a readable sequence of char values. It is implemented by the String and StringBuilder classes. Many methods in these classes accept arguments of this interface type, and specify it as their return type. This interface facilitates interoperability between these classes. It defines the following methods: int length() Returns the…
-
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…