在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理Java知识点的第六篇博客。
本篇博客介绍了关于Java的方法。
本系列博客所有Java代码都使用IntelliJ IDEA编译运行,版本为2022.1。所用JDK版本为JDK11。
目录
方法
初识方法
带参数方法
带返回值的方法
方法的注意事项
方法重载
方法参数传递注意事项
数组遍历
数组最大值
方法
初识方法
方法是将具有一些功能的代码块组合成一个整体,使其具有特殊功能的代码块。
方法必须先创建后使用(方法定义),方法创建后不直接执行,必须手动使用后执行,该过程称为方法调用。
方法定义的格式是
public static 返回类型 方法名(参数列表){
具体语句
}
没有返回值则返回类型为void。参数以逗号分隔。没有参数则括号为空。
方法调用的格式是方法名(参数);参数以逗号分隔。没有参数则括号为空。
main方法是Java程序的入口。
public class method1 {
public static void main(String[] args){
judge();
}
public static void judge(){
int number = 15;
if(number % 2 ==0){
System.out.println("It is even");
}else{
System.out.println("It is odd");
}
}
}
程序输出为It is odd
程序main方法中调用judge方法,judge方法根据judge方法内number的值输出对应语句。
public class method2 {
public static void main(String[] args){
getmax();
}
public static void getmax(){
int x = 10;
int y = 12;
if(x > y){
System.out.println("The max is " + x);
}else{
System.out.println("The max is " + y);
}
}
}
程序输出为The max is 12
程序main方法调用getmax方法,getmax方法将本方法内两个变量进行比较并输出最大值。
带参数方法
定义带参数方法时,方法名后括号内为:
数据类型1 变量1,数据类型2 变量2,数据类型3 变量3...
定义时数据类型和变量名都不能少。
调用带参方法时,括号内是逗号分隔的多个参数,参数可以是常量,也可以是变量。
方法调用时参数的数量和类型必须与方法定义时一一匹配,否则报错。
public class method3 {
public static void main(String[] args){
judge(10);
int number =15;
judge(number);
}
public static void judge(int x){
if(x % 2 ==0){
System.out.println(x + " is even");
}else{
System.out.println(x + " is odd");
}
}
}
这段代码的judge方法从main方法中获取一个整数,并判断奇偶数。
输出结果为
10 is even
15 is odd
方法定义中的参数是形式参数,简称形参。方法调用时的参数是实际参数,简称实参。
public class method4 {
public static void main(String[] args){
getmax(5,10);
int a = 15;
int b = 12;
getmax(a,b);
}
public static void getmax(int a , int b){
if(a > b){
System.out.println("The max is " + a);
}else{
System.out.println("The max is " + b);
}
}
}
这段代码中的getmax方法获取两个整数,并输出最大值。
输出结果:
The max is 10
The max is 15
带返回值的方法
返回值是方法结束时返回的数据。
方法用return返回一个数,返回值应该与定义时的类型匹配,否则程序报错。
方法的返回值可以用变量接收,如果不接收就没有意义。
import java.util.Scanner;
public class method5 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
boolean flag = judge(number);
System.out.println(flag);
}
public static boolean judge(int x){
if(x % 2 == 0){
return true;
}else{
return false;
}
}
}
这段代码从main方法中获取输入,并在judge方法中判断是奇数还是偶数,将结果用boolean类型返回main方法输出。
下面是输入10时的结果
10
true
下面是输入11时的结果
11
false
方法的注意事项
方法不能嵌套定义。void是无返回值类型,对应方法可以省略return,也可以写return;
方法重载
当同一个类中的多个方法满足方法名相同,且参数的数量,类型或顺序不同时,就构成了重载。
重载仅对应方法的定义,与方法的调用无关,调用的格式不变。
重载与返回值无关,不能通过返回值判断是否重载。
public class overload1 {
public static void main(String[] args){
int result1 = add(10, 15);
System.out.println(result1);
double result2 = add(6.6, 8.8);
System.out.println(result2);
int result3 = add(4, 3, 5);
System.out.println(result3);
}
public static int add(int a, int b){
return a + b;
}
public static double add(double a, double b){
return a + b;
}
public static int add(int a, int b, int c){
return a + b + c;
}
}
这段代码的三个add方法构成重载。都是求和并返回,不过参数类型和数量有所不同。程序的输出是:
25
15.4
12
public class overload2 {
public static void main(String[] args){
boolean flag1 = compare((byte)10,(byte)12);
System.out.println(flag1);
boolean flag2 = compare((short)15,(short)12);
System.out.println(flag2);
boolean flag3 = compare(10,10);
System.out.println(flag3);
boolean flag4 = compare((long)12,(long)10);
System.out.println(flag4);
}
public static boolean compare(byte a,byte b){
if(a == b){
return true;
}else{
return false;
}
}
public static boolean compare(short a,short b){
if(a == b){
return true;
}else{
return false;
}
}
public static boolean compare(int a,int b){
if(a == b){
return true;
}else{
return false;
}
}
public static boolean compare(long a,long b){
if(a == b){
return true;
}else{
return false;
}
}
}
这段代码的4个compare方法构成重载,分别接受两个byte,short,int,long类型的数据,比较是否相等,并将结果返回。
输出结果是:
false
false
true
false
方法参数传递注意事项
对于基本数据类型的参数,形参改变不影响实参。
public class transport1 {
public static void main(String[] args){
int number = 10;
System.out.println("number = " + number);
change(number);
System.out.println("number = " + number);
}
public static void change(int number){
number = 20;
}
}
这段代码中,main方法内的number变量值为10,虽然作为实参传递给了change方法,并且change方法中已经将形参修改为20,但是main方法内的number值还是10。
程序的输出为:
number = 10
number = 10
对于引用类型,形参的修改会影响实参。
public class transport2 {
public static void main(String[] args){
int[] arr = new int[3];
arr[0] = 10;
arr[1] = 12;
arr[2] = 15;
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
change(arr);
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
}
public static void change(int[] arr){
arr[0] = 16;
arr[1] = 18;
arr[2] = 20;
}
}
这段代码中,main方法内定义的arr数组被传递给change方法,change方法中修改形参的值,在回到main方法后,arr的值改变。
输出结果为:
10
12
15
16
18
20
数组遍历
public class arraylook {
public static void main(String[] args){
int[] arr = new int[]{11,22,33,44,55};
print(arr);
}
public static void print(int[] arr){
int i;
for(i = 0; i < arr.length ; i += 1){
System.out.print(arr[i] + " ");
}
}
}
这段代码将数组遍历移到了一个方法里。输出是:
11 22 33 44 55
System.out.print和System.out.println的区别是前者输出结束后不换行。
System.out.println()换行。
数组最大值
public class arraymax {
public static void main(String[] args){
int[] arr = {12,63,98,43,50};
int max = getmax(arr);
System.out.println("max is " + max);
}
public static int getmax(int[] arr){
int i;
int max = arr[0];
for(i = 0;i < arr.length; i+=1){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
}
getmax方法接收一个数组,并获取最大值,将结果返回。
输出结果是:
max is 98



