rewrite java for better undestanding