+ 1
why we use returne value in methods ? plz help
2 Answers
+ 8
In fact, When compiler read return statment on a part of method, stop method.
When we want the return have not value, using void return-type for declaring method,
else We need to define a return-type for method (example int, double, etc.)
â------------------------------------
void: it's optional to using return; in method body
// Example 1
void write(String text) {
System.out.println("-->" + text);
}
// Example 2
void write(String text) {
if (text.equals("bye")) {
return; // Just for stop method
}
System.out.println("-->" + text);
}
NOTE: We can use return without value, just for stop method.
â------------------------------------
no void: It's Mandatory to using return with value in method body, and method must have return value.
// Exmple 3
String getAnswer(String text) {
if (text.equals("bye")) {
return "See You";
}
return "My english is not very well.";
}
NOTE: Finally,we must return a value from method when method return-tyoe isn't void.
+ 1
thank you my bro for help đ