1、方法
定义格式:
public static void 方法名() {
方法体
}
调用格式:
方法名();
public class Demo01Method {
public static void main(String[] args) {
for (int j = 0; j < 5; j++) {
for (int i = 1; i<=20; i++){
System.out.print("*");
}
System.out.println();
}
}
}
返回值类型为void的方法没有返回值 只能单独调用
public class Demo03MethodReturn {
public static void main(String[] args) {
printMethod(5, 6);
//System.out.println(printMethod(5,6);); //没有返回值return
}
public static void printMethod(int a, int b) { //不能return
int result = a + b;
System.out.println(result);
}
}
方法重载
- 参数个数不同
- 参数类型不同
- 参数多类型顺序不同
与返回值类型,参数名等因素无关
public class Demo04MethodReload {
public static void main(String[] args) {
System.out.println(sum(1,2.0));
System.out.println(sum(1, 2, 3));
System.out.println(sum(1.0, 2));
System.out.println(sum(1.0, 2, 3));
}
public static int sum(int a, double b) {
return (int) (a + b);
}
public static int sum(int a, int b, int c) {
System.out.println("三个int参数");
return a + b + c;
}
public static int sum(double a, int b) {
return (int) (a + b);
}
public static int sum(double a, int b, int c) {
System.out.println("三个int参数");
return (int) (a + b + c);
}
}
2、数组
特点
- 一种引用数据类型
- 类型统一
- 数组长度在程序运行期间不可变
2.1、初始化
2.1.1、动态(指定长度):数据类型[] 数组名 = new 数据类型[指定长度]
int[] arrayA = new int[300];
double[] arrayB = new double[10];
String[] arrayC = new String[5];
也可简写
2.1.2、静态(指定内容):数据类型[] 数组名 = new 数据类型[] {元素1, 元素2, …} 自动识别数组长度
int[] arrayA = new int[] {1, 2, 3, 4};
String[] arrayB = new String[] {"hello", "world"};
可省略为:数据类型[] 数组名 = {元素1, 元2, …}省略后不能简写
int[] arryA = {1, 2, 3, 4}
也可拆分为两个步骤
double[] arrayC;
arrayC = new double[] {1, 2, 3, 4};
已经确定了内容最好用静态初始化
2.2、java数组的内存划分
2.2.1、5模块
-
栈(stack)
存放方法中的局部变量,也是方法运行的唯一位置。一旦超出作用域,立即从栈内存中消失
-
堆(Heap)
new出来的对象全在堆中
堆内存里的内容都有一个地址值:16进制
堆内存里的数据都有默认值
整数默认0
浮点数0.0
字符‘\u0000’
布尔false
引用类型null -
方法区(Method Area)
存储.class相关信息,包含方法 信息
-
本地方法 (Native Method Stack)
与操作系统相关
-
寄存器(PC Register)
2.2.2、一个数组
进入栈内存:方法区中的main方法加载到stack中,并为main方法在stack中开辟一个内存空间
在stack中main方法的内存空间中定义数据类型
new出来的数组放在heap内存中开辟的空间
变量在栈中,它存储的是堆中的内存地址,这就是java中的引用
2.2.3、两个数组
两个独立的数组
两个引用地址相同的数组,相互影响
2.3、数组操作
- 数组长度
System.out.println(arrayA.length);
- 数组遍历
for (int i = 0; i < arrayA.length; i++) {
System.out.println(arrayA[i]);
}
- 将数组作为方法参数,传给方法
public class Demo03ArrayReverse {
public static void main(String[] args){
int[] array = {1, 2, 3, 4, 5};
printArray(array);
}
public static void printArray(int[] array) {
System.out.println("地址"+array);
}
}
- 数组作为方法的返回值,返回给对象
public class Demo03ArrayReverse {
public static void main(String[] args){
int[] result = calculate(1,2,3);
System.out.println(result[1]);
System.out.println(result[0]);
}
public static int[] calculate(int a, int b, int c) {
int sum = a + b + c;
int avg = sum/3;
int[] arrayA = {sum, avg};
return arrayA;
}
}
- 数组的复制 (详见java常见API(System类))
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
:将数组中指定的数据拷贝到另一个数组中。
数组的拷贝动作是系统级的,性能很高。System.arraycopy方法具有5个参数,含义分别为:
参数序号 | 参数名称 | 参数类型 | 参数含义 |
---|---|---|---|
1 | src | Object | 源数组 |
2 | srcPos | int | 源数组索引起始位置 |
3 | dest | Object | 目标数组 |
4 | destPos | int | 目标数组索引起始位置 |
5 | length | int | 复制元素个数 |
2.4 二维数组
初始化二维数组
- 只分配空间
- 只分配二维数组空间
- 指定内容同时分配空间
int[][] a = new int[2][3]; //有两个一维数组,每个一维数组的长度是3
a[1][2] = 5; //可以直接访问一维数组,因为已经分配了空间
//只分配了二维数组
int[][] b = new int[2][]; //有两个一维数组,每个一维数组的长度暂未分配
b[0] =new int[3]; //必须事先分配长度,才可以访问
b[0][2] = 5;
//指定内容的同时,分配空间
int[][] c = new int[][]{
{1,2,4},
{4,5},
{6,7,8,9}