Java version of code