Category: Character Case in a String
-
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…
-
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…
-
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…
-
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…
-
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’); //…
-
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…
-
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…
-
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…