Friday, 15 March 2013

java - Must I have a return statement in a method when I already have it in the if statement? -



java - Must I have a return statement in a method when I already have it in the if statement? -

i got function below:

public double getcost(string bookname) { if(bookname.equals(arrayofbook[2])) { homecoming arrayofcost[2]; } }

arrayofcost array of double. have statement homecoming double value here. compiler still says missing homecoming statement in function. that's why?

yes, need homecoming statement (in fact, compiler tells so). because method must homecoming regardless of condition. in other words, programme needs specify happens when status false. example, homecoming zero, infinity, "not number" constant, et cetera.

the exception void methods: not require homecoming statement @ end, , allowed have returns in other branches.

java

No comments:

Post a Comment