common algorithm described by JAVA