Author: Martha Sandoval
-
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 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…
-
Advantages of Exception Handling – Exception Handling
7.8 Advantages of Exception Handling Robustness refers to the ability of a software system to respond to errors during execution. A system should respond to unexpected situations at runtime in a responsible way. Applications that provide the user with frequent cryptic messages with error codes or that repeatedly give the user the silent treatment when…
-
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 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’); //…
-
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…
-
Concise try-with-resources Statement – Exception Handling
Concise try-with-resources Statement The header of the try-with-resources statement can be made less verbose by factoring out the resource declarations from the header. This refactoring involves declaring the resources preceding the try statement and specifying instead the resource variables in the header. The resources must be AutoCloseable (to provide the close() method) as before, but…
-
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…