Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: wordsmithing

...

The character and string escape sequences allow for the representation of some nongraphic characters as well as the single quote, double quote, and backslash characters in character literals (§3.10.4) and string literals (§3.10.5).

Correctly Correct use of escape sequences in String literals depends on correct understanding of how the escape sequences are interpreted. SQL statements written in Java, for example, sometimes require certain escape characters or sequences (e.g., sequences containing \t, \n, \r). When representing SQL queries in Java String form, all escape sequences must be preceded by an extra backslash for correct interpretation.

...

This noncompliant code example defines a method splitWords() that finds matches between the String literal and the input sequence. The programmer believes that Java that String literals can be used as is for regular expression patterns. Consequently, he initializes the string WORDS to "\b", expecting that the string literal will hold the escape sequence for matching a word boundary. However, the Java compiler treats the "\b" as a Java escape sequence, and the string WORDS silently compiles to a backspace character.

Code Block
bgColor#FFCCCC
public class BadSplitter {
  private final String WORDS = "\b"; // The Intent was to split on word boundaries

  public String[] splitWords(String input){
    Pattern p = Pattern.compile(WORDS);
    String[] input_array = p.split(input);
    return input_array;
  }
}

...