In Java a function is called a method, because it always belongs to a class.
Writing a method#
public class Main {
static int square(int x) {
return x * x;
}
static void greet(String name) {
System.out.println("Hello, " + name);
}
public static void main(String[] args) {
System.out.println(square(7)); // 49
greet("Asha");
}
}
- The return type comes first:
int,String, orvoidfor nothing. - Each parameter has a type.
staticmeans the method belongs to the class itself, somaincan call it directly. Methods withoutstaticbelong to objects, which you meet with classes.
Order does not matter in Java: a method can be called above where it is defined.
return#
return hands back a value and ends the method. A non-void method must return a value on every path; the compiler checks.
static String sign(int n) {
if (n > 0) return "positive";
if (n < 0) return "negative";
return "zero";
}
Overloading#
Several methods can share a name if their parameter lists differ:
static int max(int a, int b) { return a > b ? a : b; }
static double max(double a, double b) { return a > b ? a : b; }
static int max(int a, int b, int c) { return max(max(a, b), c); }
Java picks the one that matches the arguments.
How arguments are passed#
Java always passes a copy of the value. For primitives, that is a copy of the number, so the caller's variable cannot change:
static void reset(int x) { x = 0; }
int n = 5;
reset(n); // n is still 5
For arrays and objects, the value copied is a reference: both the caller and the method point at the same array. So changes to its contents are visible to the caller:
static void zeroFirst(int[] nums) {
nums[0] = 0;
}
int[] a = {5, 6};
zeroFirst(a); // a is now {0, 6}
But pointing the parameter at a new array does not affect the caller:
static void replace(int[] nums) {
nums = new int[] {9, 9}; // only the method's copy now points elsewhere
}
Returning more than one value#
Return an array for a quick pair, or, more clearly, a record:
record MinMax(int min, int max) {}
static MinMax minMax(int[] nums) {
int lo = nums[0], hi = nums[0];
for (int x : nums) {
lo = Math.min(lo, x);
hi = Math.max(hi, x);
}
return new MinMax(lo, hi);
}
MinMax r = minMax(new int[] {4, 9, 1});
System.out.println(r.min() + " " + r.max()); // 1 9
A record is a small class that just holds named values.
Recursion#
static long factorial(int n) {
if (n <= 1) return 1; // base case
return n * factorial(n - 1);
}
Without the base case the method calls itself until Java throws StackOverflowError.
On CodexBash#
Solutions here are an ordinary method inside class Solution, without static. The judge creates the object and calls your method, so you write only the method, plus any helpers beside it in the same class.
Key points#
- Methods declare a return type and typed parameters;
voidreturns nothing. staticmethods can be called frommaindirectly.- Overloading reuses a name for different parameter lists.
- Arguments are copied; for arrays the copy is a reference, so contents can change.
- Recursion needs a base case.
Next: arrays and ArrayList.