Thinking in Java (Chapter 13)
===
###### tags: `Java` `OOP`
1. Objects of the String class are immutable
1. The ‘+’ and ‘+=‘ for String are the only operators that are overloaded in Java, and Java does not allow the programmer to overload any others.
1. javap -c Concatenation
1. the compiler creates a StringBuilder object to build the String s, and calls append( )
1. if looping is involved, you should explicitly use a StringBuilder in your toString( ). If you try to take shortcuts and do something like append(a + ": " + c), the compiler will jump in and start making more StringBuilder objects
1. StringBuilder was introduced in Java SE5. Prior to this, Java used StringBuffer, which ensured thread safety
1. format specifiers
1. The opposite of width is precision, which is used to specify a maximum
1. For Strings, the precision specifies the maximum number of characters from the String to print. For floating point numbers, precision specifies the number of decimal places to display (the default is 6), rounding if there are too many or adding trailing zeroes if there are too few. Since integers have no fractional part, precision isn’t applicable to them
1. %[argument_index$][flags][width][.precision]conversion
1. In Java, ‘ \ \ ‘ means "I’m inserting a regular expression backslash, so that the following character has special meaning." For example, if you want to indicate a digit, your regular expression string will be ‘\\d’. If you want to insert a literal backslash, you say ‘\\\\’- However, things like newlines and tabs just use a single backslash: ‘\n\t’.
1. to say that a number might or might not be preceded by a minus sign, you put in the minus sign followed by a question mark, like this: -?
1. (-I\\+)? means that this part of the string may be either a ‘-’ or a ‘+’ or nothing (because of the ‘?’)
1. ‘\W’, which means a non-word character (the lowercase version, ‘\w’, means a word character)
1. Greedy: Quantifiers are greedy unless otherwise altered. A greedy expression finds as many possible matches for the pattern as possible
1. Reluctant: Specified with a question mark, this quantifier matches the minimum number of characters necessary to satisfy the pattern
interface CharSequence
{charAt(int i);
length();
subSequence(int start,| int end);
toString();}
1. import java.util.regex
1. Pattern.compile( )
1. Import java.util.regex, then compile a regular expression by using the static Pattern.compile( ) method. This produces a Pattern object based on its String argument. You use the Pattern by calling the matcher( ) method, passing the string that you want to search. The matcher( ) method produces a Matcher object, which has a set of operations to choose from
1. find( ) will locate the regular expression anywhere in the input, but lookingAt( ) and matches( ) only succeed if the regular expression starts matching at the very beginning of the input.
1. Invoking either start( ) or end( ) following an unsuccessful matching operation (or before attempting a matching operation) produces an IllegalStateException
1. You can combine the effect of these and other flags through an "OR" (‘|’) operation