Junior — Middle
What type of data is returned when using the return operator in a function or method?
sobes.tech AI
Answer from AI
In Android (Java/Kotlin), the return operator in a function or method returns a value of the type specified in the function declaration. For example, if a method is declared as int getNumber(), then the return operator must return a value of type int.
If the method is declared with a return type of void, then the return operator is used without a value, simply to exit the method.
Example in Java:
public int sum(int a, int b) {
return a + b; // returns an int
}
public void printHello() {
System.out.println("Hello");
return; // exits the method without a return value
}
Thus, the return type corresponds to the declared type of the method or function.