I'm not exactly sure, but if I wrote something similar, it would look like your first example.
Deepakk Verma wrote:my first example is compiling but second one is not.......
"This method must return a result of type boolean"
JVM complains because, there is no way to know that your if condition will be true. If that is not true, you are not returning anything as against method expectaton to return boolean
That is not only shorter, but much more logical. You return the result, rather than messing about with if-elses. There is less risk for error if there is less code. That is why this is the recommended format here.
D. Ogranos wrote:Btw. if you have such a condition, you can write the method shorter . . .
To answer the question you asked, rather than the question you thought you asked:
Deepakk Verma wrote:if we have a method with return type boolean then is it compulsory to have both true and false returns . . .
You can have a method which always returns true or false. It is a bit useless, but is legal syntax.The reason you had problems with your first example which didn't compile is that there were circumstances where the method returned nothing at all. If you give "foo" as the return type, the method must return a "foo" from every path.