String manipulation in Java
Just recoded it, doing simply what your explanation said 


  Except keep the first char if it is 'a' and keep the second char if
  it is 'b'.


Since a possible deletion of a character will affect indices, we check the second character before we check the first.
The resulting code is much simpler and readable simply because the extracted method deleteCharIfNotEqualTo() does away with some code duplication and at the same time heightens readability of the deFront() method, by explaining what is happening.

public String deFront(String str) {
    StringBuilder builder = new StringBuilder(str);
    deleteCharIfNotEqualTo(builder, 1, 'b');
    deleteCharIfNotEqualTo(builder, 0, 'a');
    return builder.toString();
}

private void deleteCharIfNotEqualTo(StringBuilder builder, int index, char character) {
    if (builder.length() &gt; index &amp;&amp; character != builder.charAt(index)) {
        builder.deleteCharAt(index);
    }
}