一、方法的概念
方法(method)是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集 注意:
方法必须先创建才可以使用,该过程称为方法定义 方法创建后并不是直接可以运行的,需要手动使用后,才执行,该过程称为方法调用
二、方法的定义和调用
无参数方法定义和调用
定义格式: public static void 方法名 ( ) { // 方法体; }
调用格式:方法名();
注意:方法必须先定义,后调用,否则程序将报错
public class Demo1Method {
public static void main ( String [ ] args) {
eat ( ) ;
}
public static void eat ( ) {
study ( ) ;
System . out. println ( "吃饭" ) ;
}
public static void study ( ) {
System . out. println ( "学习" ) ;
}
}
方法的调用过程
程序开始运行,.class字节码文件会进入方法区,其中包含了所定义的方法,包含主方法main 每个方法在被调用执行的时候,都会进入栈内存,并且拥有自己独立的内存空间,方法内部代码调用 完毕之后,会从栈内存中弹栈消失(FILO,first in last out,先进后出)
public class Demo2Method {
public static void main ( String [ ] args) {
method ( ) ;
}
public static void method ( ) {
int num = 11 ;
if ( num % 2 == 0 ) {
System . out. println ( "偶数" ) ;
} else {
System . out. println ( "奇数" ) ;
}
}
}
三、带参数方法的定义和调用
定义格式:
public static void 方法名 (参数1) {方法体;}
public static void 方法名 (参数1, 参数2, 参数3...) {方法体; }
调用格式
注意事项
方法定义时,参数中的数据类型与变量名都不能缺少,缺少任意一个程序将报错 方法定义时,多个参数之间使用逗号( ,)分隔 方法调用时,参数的数量与类型必须与方法定义中的设置相匹配,否则程序将报错.
public class Demo1Method {
public static void main ( String [ ] args) {
isEvenNumber ( 10 ) ;
}
public static void isEvenNumber ( int num) {
if ( num % 2 == 0 ) {
System . out. println ( "偶数" ) ;
} else {
System . out. println ( "奇数" ) ;
}
}
}
形参和实参
方法定义中的参数,等同于变量定义格式,例如:int number 实参:方法调用中的参数,等同于使用变量或常量,例如: 10 number 带参数方法案例:打印n-m之间所有的奇数
public class Demo2Method {
public static void main ( String [ ] args) {
print ( 20 , 10 ) ;
}
public static void print ( int n, int m) {
System . out. println ( n + "到" + m + "之间的奇数为:" ) ;
for ( int i = 20 ; i <= 10 ; i++ ) {
if ( i % 2 == 1 ) {
System . out. println ( i) ;
}
}
}
}
四、带返回值方法的定义和调用
定义格式
方法定义时return后面的返回值与方法定义上的数据类型要匹配,否则程序将报错
public static 数据类型 方法名 ( 参数 ) {
return 数据 ;
}
调用格式
方法的返回值通常会使用变量接收,否则该返回值将无意义
方法名 ( 参数 ) ;
数据类型 变量名 = 方法名 ( 参数 ) ;
public class Demo2Method {
public static void main ( String [ ] args) {
System . out. println ( getMax ( 10 , 20 ) ) ;
int result = getMax ( 10 , 20 ) ;
System . out. println ( result) ;
for ( int i = 1 ; i <= result; i++ ) {
System . out. println ( "HelloWorld" ) ;
}
}
public static int getMax ( int a, int b) {
if ( a > b) {
return a;
} else {
return b;
}
}
}
五、方法重载
方法重载概念
方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载
多个方法在同一个类中 多个方法具有相同的方法名 多个方法的参数不相同,类型不同或者数量不同 重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,换句话说不能通过返回值来判定两个方法是否相互构成重载
public class Demo1Overload {
public static void main ( String [ ] args) {
short a = 10 ;
short b = 20 ;
System . out. println ( compare ( a, b) ) ;
}
public static boolean compare ( int a, int b) {
return a == b;
}
public static boolean compare ( byte a, byte b) {
return a == b;
}
public static boolean compare ( short a, short b) {
return a == b;
}
public static boolean compare ( long a, long b) {
return a == b;
}
}
六、方法的参数传递
public class Test1 {
public static void main ( String [ ] args) {
int number = 100 ;
System . out. println ( "调用change方法前:" + number) ;
change ( number) ;
System . out. println ( "调用change方法后:" + number) ;
}
public static void change ( int number) {
number = 200 ;
}
}
方法参数传递引用类型
引用数据类型的传参,传入的是地址值,内存中会造成两个引用指向同一个内存的效果,所以即使方法弹栈,堆内存中的数据也已经是改变后的结果
public class Test2 {
public static void main ( String [ ] args) {
int [ ] arr = { 10 , 20 , 30 } ;
System . out. println ( "调用change方法前:" + arr[ 1 ] ) ;
change ( arr) ;
System . out. println ( "调用change方法后:" + arr[ 1 ] ) ;
}
public static void change ( int [ ] arr) {
arr[ 1 ] = 200 ;
}
}
七、方法的应用案例
public class Test1 {
public static void main ( String [ ] args) {
int [ ] arr = { 11 , 22 , 33 , 44 , 55 } ;
printArray ( arr) ;
System . out. println ( "另外一段代码逻辑 " ) ;
}
public static void printArray ( int [ ] arr) {
System . out. print ( "[" ) ;
for ( int i = 0 ; i < arr. length; i++ ) {
if ( i == arr. length - 1 ) {
System . out. println ( arr[ i] + "]" ) ;
} else {
System . out. print ( arr[ i] + ", " ) ;
}
}
}
}
public class Test2 {
public static void main ( String [ ] args) {
int [ ] arr = { 11 , 55 , 22 , 44 , 33 } ;
int max = getMax ( arr) ;
System . out. println ( max) ;
}
public static int getMax ( int [ ] arr) {
int max = arr[ 0 ] ;
for ( int i = 1 ; i < arr. length; i++ ) {
if ( max < arr[ i] ) {
max = arr[ i] ;
}
}
return max;
}
}
public class Test3 {
public static void main ( String [ ] args) {
int [ ] arr = { 11 , 55 , 33 , 22 , 44 } ;
int [ ] maxAndMin = getMaxAndMin ( arr) ;
System . out. println ( maxAndMin[ 0 ] ) ;
System . out. println ( maxAndMin[ 1 ] ) ;
}
public static int [ ] getMaxAndMin ( int [ ] arr) {
int max = arr[ 0 ] ;
for ( int i = 1 ; i < arr. length; i++ ) {
if ( max < arr[ i] ) {
max = arr[ i] ;
}
}
int min = arr[ 0 ] ;
for ( int i = 1 ; i < arr. length; i++ ) {
if ( min > arr[ i] ) {
min = arr[ i] ;
}
}
int [ ] maxAndMin = { min, max} ;
return maxAndMin;
}
}