In Java, a method can have at most one return value. This means that a method can return a single value of a specific data type, or it can return void
if it does not return a value.
Here's an example of a method that returns an int
value:
public int add(int a, int b) { return a + b; }Source:www.lautturi.com
This method takes two int
arguments and returns their sum as an int
value.
Here's an example of a method that does not return a value (a void
method):
public void printHello() { System.out.println("Hello!"); }
This method does not take any arguments and does not return a value. It simply prints the string "Hello!" to the console.
Keep in mind that a method can return a value of any data type, as long as it is compatible with the method's return type. For example, a method can return a String
, a double
, a boolean
, or any other data type.
However, a method can only return a single value. If you want a method to return multiple values, you will need to use an alternative approach such as returning an array or an object that contains the multiple values.