12.10.4. Making Assertions Required
Sometimes (rarely) you may need to ensure that some class never has its assertions turned off. You can do this
with code like the following
static {
boolean assertsEnabled = false;
assert assertsEnabled = true;
if (!assertsEnabled)
throw new IllegalStateException("Asserts required");
}
In this code we purposefully use a side effect of the assert statement so that the code can determine
whether asserts have been turned offif they have then the class will not load. This is a nearly unique case in
which side effects are a good thing.
Requiring assertions to be on complicates the running environment of the code since it must always turn on
assertions for this class even if they are off elsewhere. You should do this almost, if not actually, never.
The greatest of all faults is to be conscious of none.
Thomas Carlyle
Chapter 13. Strings and Regular Expressions
What's the use of a good quotation if you can't change it?
Dr. Who, The Two Doctors
Strings are standard objects with built-in language support. You have already seen many examples of using
string literals to create string objects. You've also seen the + and += operators that concatenate strings to
create new strings. The String class, however, has much more functionality to offer. String objects are
immutable (read-only), so you also have a StringBuilder class for mutable strings. This chapter
describes String and StringBuilder and some related classes, including utilities for regular expression
matching.
13.1. Character Sequences
As described in "Character Set" on page 161, the Java programming language represents text consisting of
Unicode characters as sequences of char values using the UTF-16 encoding format. The String class
defines objects that represent such character sequences. More generally, the java.lang.CharSequence
interface is implemented by any class that represents such a character sequencethis includes the String,
StringBuilder, and StringBuffer classes described in this chapter, together with the
java.nio.CharBuffer class that is used for performing I/O.
The CharSequence interface is simple, defining only four methods:
public charcharAt(int index)