r/learnjava • • Aug 06 '21

Doubts regarding the return statements

can someone explain me why this method returns a Boolean without me actually using a "return boolean" statement in there?

public boolean doesArrayContainThisValue(int searchValue) {

boolean valueInArray = false;

for(int i = 0; i < arraySize; i++){

if(theArray[i]==searchValue){

valueInArray = true;

}

3 Upvotes

6 comments sorted by

6

u/CelticHades Aug 06 '21

Isn't it giving an error? It must be giving error for lack of return statement

0

u/BerserkGamer_YT Aug 06 '21

it didn't 8:10 this is where i got it from

5

u/New-Abbreviations152 Aug 06 '21

At 8:38, you can clearly see that the method returns valueInArray, as it should. Are you sure it doesn't give you an error when you try to copy the code?

0

u/BerserkGamer_YT Aug 06 '21 edited Aug 06 '21

well it did work for him so i just wanted to know how it worked

7

u/CelticHades Aug 06 '21

It worked for him because he returned boolean value.

4

u/BerserkGamer_YT Aug 06 '21

i missed that part he actually returned it in 8:38 sorry guys. thanks for all the help