Java SE 7 Programmer II (1Z0-804) notes

This post contains a collection of notes that I have made when I was preparing for the Java SE 7 Programmer II exam. For tips on how to prepare for this exam please see my other post.

Java SE 7 Programmer II notes

String processing

  • The delimiter string itself doesn’t act as a delimiter but each character in the delimiter String acts as a delimiter. So StringTokenizer(“abcde”, “bd”) will return a, c, and e and not abcde because b and d both act as a delimiter.
  • When using “\s” as tokenizer, don’t forget to add additional backslash to escape the “\”, i.e., you should use “\\s”.
  • The word boundary is indicated by “\\b” and includes spaces, tabs, special characters and beginning and end of the line.
  • When using explicit index (e.g., %1$) the index number always starts with 1.
  • String is immutable while StringBuilder and StringBuffer are not.
  • For regular expressions the word boundary token “\b” does not actually match any character. For example the matches differ if you match with \btest\b or with \stest\s for the string “test test test test”. The first pattern will give 4 matches while the second will give only 1 match (the second “test”).
  • When using split(String regex) the trailing empty strings won’t be included.
  • The default precision for floating point number is 6.
  • When padding is specified the width needs to be specified as well, otherwise an exception is thrown.
  • The parameter %f only accepts floats or doubles, it doesn’t accept booleans, integers, chars or anything else.
  • When using Scanner there are many hasNextXXX and nextXXX methods, except for hasNextCharacter and nextCharacter, they do not exist!
  • Note that for word boundaries (when using \b as pattern) the parts before and after a word are also considered as word boundaries, even if there is no visible boundary such as white space. This means that when you use the pattern \b\w*\b on “someword” it gives someword back as a match.
  • Calendar has three important methods:
    • Add(int aField, int amount).
    • Roll(int aField, int amount), is quite similar to add(…). However, it does not modify the “larger” fields, i.e., if you add 11 months to the date in March 2014 then it would yield Februari 2014 and not Februari 2015, hence the larger fields (in this case year) is not modified.
    • Set(int aField, int value).

Continue reading