Java 方法
方法的定义与调用、void、值传递、重载、可变参数与递归。
方法的定义
A method is modifiers, return type, name, parameter list and body. void means nothing is returned; with a real return type every path must return.
public class Define {
public static int max(int a, int b) {
return a > b ? a : b;
}
public static void greet(String name) {
System.out.println("Hello, " + name);
}
public static void main(String[] args) {
System.out.println(max(3, 7));
greet("Tom");
}
}7
Hello, Tom方法调用
Call static methods as Class.method() and instance methods as object.method(). Inside the same class the class name may be omitted, though writing it aids readability.
public class Call {
static int square(int x) {
return x * x;
}
int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(square(4));
System.out.println(Call.square(5));
Call c = new Call();
System.out.println(c.add(2, 3));
}
}16
25
5void 关键字
void declares no return value; a bare return; exits early, but return value is illegal. Void methods exist for side effects such as printing or writing files.
public class VoidDemo {
static void printGrade(double score) {
if (score >= 90) {
System.out.println("A");
return;
}
System.out.println("B");
}
public static void main(String[] args) {
printGrade(95);
printGrade(80);
}
}A
B通过值传递参数
Java is pass-by-value only. Primitives pass a copy of the value, so changes stay local; references pass a copy of the address, so mutating the object is visible outside while reassigning is not.
public class PassByValue {
static void change(int x) {
x = 99;
}
static void mutate(int[] arr) {
arr[0] = 99;
}
static void rebind(int[] arr) {
arr = new int[]{7};
}
public static void main(String[] args) {
int a = 1;
change(a);
System.out.println(a);
int[] b = {1, 2};
mutate(b);
System.out.println(b[0]);
rebind(b);
System.out.println(b[0]);
}
}1
99
99方法重载
Same name with a different parameter list (count, types or order) is overloading. The compiler picks by argument types; the return type does not participate.
public class Overload {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
System.out.println(add(1, 2));
System.out.println(add(1.5, 2.5));
System.out.println(add(1, 2, 3));
}
}3
4.0
6可变参数
Type... name accepts any number of arguments of that type and is seen as an array inside. It must be the last parameter and there can be only one.
public class Varargs {
static int sum(int... nums) {
int total = 0;
for (int n : nums) {
total += n;
}
return total;
}
public static void main(String[] args) {
System.out.println(sum());
System.out.println(sum(1));
System.out.println(sum(1, 2, 3));
System.out.println(sum(new int[]{4, 5}));
}
}0
1
6
9递归
Recursion is a method calling itself; it needs a base case that terminates, otherwise you get StackOverflowError. Decide what the smallest case returns and how each call shrinks the problem.
public class Recursion {
static int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
static int fib(int n) {
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
}
public static void main(String[] args) {
System.out.println(factorial(5));
System.out.println(fib(10));
}
}120
55方法的定义
方法由修饰符、返回值类型、方法名、参数列表和方法体组成。返回值类型为 void 表示不返回任何值;有返回值时每条路径都必须 return。
public class Define {
public static int max(int a, int b) {
return a > b ? a : b;
}
public static void greet(String name) {
System.out.println("Hello, " + name);
}
public static void main(String[] args) {
System.out.println(max(3, 7));
greet("Tom");
}
}7
Hello, Tom方法调用
静态方法用 类名.方法() 调用,实例方法用 对象.方法()。同类内部调用静态方法可以省略类名,但显式写出来更易读。
public class Call {
static int square(int x) {
return x * x;
}
int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(square(4));
System.out.println(Call.square(5));
Call c = new Call();
System.out.println(c.add(2, 3));
}
}16
25
5void 关键字
void 声明方法不返回值,方法体里可以写 return; 提前结束,但不能 return 值。void 方法常用来做副作用操作,如打印、写文件。
public class VoidDemo {
static void printGrade(double score) {
if (score >= 90) {
System.out.println("A");
return;
}
System.out.println("B");
}
public static void main(String[] args) {
printGrade(95);
printGrade(80);
}
}A
B通过值传递参数
Java 只有值传递。基本类型传的是数值副本,方法内改动不影响外面;引用类型传的是地址副本,方法内改对象内容会影响外面,但改指向不影响。
public class PassByValue {
static void change(int x) {
x = 99;
}
static void mutate(int[] arr) {
arr[0] = 99;
}
static void rebind(int[] arr) {
arr = new int[]{7};
}
public static void main(String[] args) {
int a = 1;
change(a);
System.out.println(a);
int[] b = {1, 2};
mutate(b);
System.out.println(b[0]);
rebind(b);
System.out.println(b[0]);
}
}1
99
99方法重载
同一个类里方法名相同、参数列表不同(个数、类型或顺序)就是重载。编译器按实参类型选择版本,返回值类型不参与重载判断。
public class Overload {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
System.out.println(add(1, 2));
System.out.println(add(1.5, 2.5));
System.out.println(add(1, 2, 3));
}
}3
4.0
6可变参数
类型... 参数名 让方法接受任意个同类型实参,在方法内部它就是一个数组。可变参数必须是参数列表的最后一个,且一个方法只能有一个。
public class Varargs {
static int sum(int... nums) {
int total = 0;
for (int n : nums) {
total += n;
}
return total;
}
public static void main(String[] args) {
System.out.println(sum());
System.out.println(sum(1));
System.out.println(sum(1, 2, 3));
System.out.println(sum(new int[]{4, 5}));
}
}0
1
6
9递归
递归是方法调用自己,必须有一个能终止的基准情形,否则会栈溢出 StackOverflowError。写递归时先想清楚「最小情况返回什么」和「如何缩小问题」。
public class Recursion {
static int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
static int fib(int n) {
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
}
public static void main(String[] args) {
System.out.println(factorial(5));
System.out.println(fib(10));
}
}120
55