+ 1
Explain the return function in this java code to me please.
i cant figure out what the function of this return in this code,explain for me please,refer to the link below : https://code.sololearn.com/cj1kju2lPTuX if the link doesn't work,check my profile's first code. the name of code : Return array code
10 Réponses
+ 5
Wang Teng Zheng hi,
The return keyword is used to return any user-defined value, in your linked sample code when the value of i is greater then the length of array which is store in x variable then no any index can be found so it will not goes in if statement and it will return the value define by user which shown the if statement is not executed and no any results can be find
+ 4
The return keyword is used to return from a method when its execution is complete. When a return statement is reached in a method, the program returns to the code that invoked it. A method can return a value or reference type or does not return a value.
So it just use to return integer value
you can use any function to return string
Wang Teng Zheng thanks for the kind word I'm just trying to help as I know which is nothing so be awesome and if any difficulty ping me I'll loved to answer with some example and explanatory ways
have these 🍎 🍎 🍎 🍎
+ 3
Wang Teng Zheng it simply print -1 as the output which shows no any index can be possible or no index is there present in the array for i=5, because 5<5 false and like this way simply return -1
public class ExerciseA7 {
public static int a(int[] arrs,int numbers){
if (arrs == null) return -1;
int x = arrs.length;
int i = 5;
while(i < x) {
if (arrs[i] == numbers) return i;
else i = i + 1;
}
return -1;
}
public static void main(String[] args) {
int[] arrs = {5,7,3,65,1};
System.out.println("The index of array is : " + a(arrs,3));
}
}
+ 3
Wang Teng Zheng null means there is nothing present in the array in that case array will be empty so empty array has no index allocation in absent of any element so we return value -1 or any other so we can get that array is empty no index can be found
+ 2
Wang Teng Zheng go through the example explain in the link
a variable is a reference to an object. A null value thus indicates an unset reference which is no value is set at address
https://stackoverflow.com/questions/17221759/returning-null-in-a-method-whose-signature-says-return-int
0
DishaAhuja if the i is greater than x then it will not going to if statement but will go to the return -1?
but what the -1 goes to?
0
Okay,understood sir,but one more thing, the if(arrs == null) return -1
i dont know much about null..what it is?
0
DishaAhuja your explaination so details,thank you! did return only can for int? how about string
0
DishaAhuja Okay,im clearly now,tq
0
DishaAhuja can u use my code and it return the null? i cant return it even i changed a wrong element, and for return,i cant simply return string right? like
return "Hello" or the return data type is based on the method type?