How to pass parameters and return values
Declare parameters
Parameters are typed variables listed in the method declaration. Arguments supply their values at a call site.
static void greet(String name) {
System.out.println("Hello, " + name);
}
greet("Ada");
Return a value
The return type states what the method produces. return ends the method and sends a compatible value back.
static int square(int number) {
return number * number;
}
int result = square(5);
Pass several arguments
Arguments must match parameter order and compatible types.
static double total(double price, int quantity) {
return price * quantity;
}
System.out.println(total(9.5, 3));
Understand value passing
Java passes every argument by value. For an object, the copied value is a reference, so a method can mutate that object but cannot replace the caller's variable.