Java backend: fixed a potential bug and some refactoring
Java backend: fixed a bug which manifests after applying an optimization