Return statement and improved function Java code