java - Why I can't return nothing in a static method this way -
i wrote piece of code train litte bit , wanted understand why cannot use notation.
public static double retanglearea (double c, double d) { double area = c * d; if ( c < 0 || d < 0) { system.out.println("error."); return; } return area; } if call method in main() shout "error." , still calculate (c * d) negative number wanted print show in. well, know how rid of problem want understand why can't way , what's problem that.
three ways round this:
throwexception rather writing error console. write console incatchblock arguably in better position deal error.change function return type
double(note capital), , returnnullif don't want calculate value.use
java.lang.optional<t>return type.
of these, prefer , therefore recommend 1.
Comments
Post a Comment