In Java can I return a boolean value with the following:
public boolean areBothEven(int i, int j) {
return (i%2 == 0 && j%2 == 0);
}
Or do I need to surround the statement with an if and then return true and false appropriately?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, in fact doing stuff like
return xxx ? true : false;orif (xxx) return true; else return false;is generally considered redundant and therefore bad style. In your case, you might even speed things up slightly by avoiding the&&(which may incur a branch mis-prediction):