Category: Concise try-with-resources Statement
-
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…
-
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…
-
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 try-with-resources Statement – Exception Handling
7.7 The try-with-resources Statement Normally, objects in Java are automatically garbage collected at the discretion of the JVM when they are no longer in use. However, resources are objects that need to be explicitly closed when they are no longer needed. Files, streams, and database connections are all examples that fall into this category of…
-
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…
-
Creating and Initializing Strings – Selected API Classes
Creating and Initializing Strings Immutability The String class implements immutable character strings, which are read-only once the string has been created and initialized. Objects of the String class are thus thread-safe, as the state of a String object cannot be corrupted through concurrent access by multiple threads. Operations on a String object that modify 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’); //…
-
Character Case in a String – Selected API Classes
Character Case in a String Click here to view code image String toUpperCase()String toUpperCase(Locale locale)String toLowerCase()String toLowerCase(Locale locale) Note that the original string is returned if none of the characters needs its case changed, but a new String object is returned if any of the characters need their case changed. These methods delegate the character-by-character…
-
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…
-
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…