Category: Oracle Certification
-
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()…
-
Text Blocks – Selected API Classes
Text Blocks Constructing string literals that span multiple lines can be tedious using string concatenation and line terminators. Text blocks provide a better solution—avoiding having to escape tab, newline, and double-quote characters in the text, and in addition preserving indentation of a multiline string. Basic Text Blocks The string sql1 below represents a three-line SQL…
-
Indenting Lines in a String – Selected API Classes
Indenting Lines in a String The indent() method allows indentation of lines in a string to be adjusted. String indent(int n) Adjusts the indentation of each line of this string based on the value of n, and normalizes line termination characters. This string is conceptually separated into lines using the String.lines() method (p. 445). Each…
-
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…
-
Implementing the AutoCloseable Interface – Exception Handling
Implementing the AutoCloseable Interface A declared resource must provide the close() method that will be called when the resource is to be closed. This is guaranteed by the fact that the resource must implement the java.lang.AutoCloseable interface which specifies the close() method. The compiler only allows resource declarations or resource variables in the try header…
-
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…