Answer the question
In order to leave comments, you need to log in
How to correctly use return in a method if there is a branch?
Good day to all.
There was a simple basic question. Maybe someone will tell?
There is a simple code. I get a number and if the number is 1 I return a string, if the number is different then I return nothing.
public String method(int i){
if(i = 1){ return "Hey";)
} else { return null;}
}
Answer the question
In order to leave comments, you need to log in
if(i = 1){ return "Hey";)
I think the compiler doesn't like this closing brace. Because if all branches have a return, it suits him.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question