package com.test.day01;
//
public class TestParam {
public void f1(int n){
n =0;
}
public static void main(String[] args) {
TestParam testParam = new TestParam();
int n = 9;
testParam.f1(n);
System.out.println(n);
} }

思考:上面的代码运行结果输出的是0还是9  答案是9    因为调用一个方法就会在栈里面开辟空间,这时候0在一个空间 9在一个空间。那么结果不会随着方法的调用而改变

那么什么时候会改变呢?

package com.test.day01;
//
class Param{
int value;
}
public class TestParam {

  //讲Param类作为类型
public void f2(Param p){
p.value=99;
}
public static void main(String[] args) {
TestParam testParam = new TestParam();
Param pc = new Param();
pc.value=22;
testParam.f2(pc);
System.out.println(pc.value);
} }

思路:引用传递是地址的传递,这里面使用了同一个地址,也就是都指向99 所以结果是99  如果在 f2里面再次创建一个对象的话,就会在堆里面创建对象,就会有新的地址。结果就会是22

使用引用参数求5个数的和:

  

package com.test.day01;
// public class TestParam {
//创建一个求和的方法
public int num(int n1,int n2,int n3,int n4,int n5){
return n1+n2+n3+n4+n5;
}
public static void main(String[] args) {
//求5个数的和
TestParam tp = new TestParam();
int sum = tp.num(88, 99, 54, 78, 90);
System.out.println(sum);
}
}

上述的代码虽然完成5个数的求和,但是如果遇到很多个数求和那么显然这样的方式是不可取的。那么怎么解决呢?传参的时候使用引用数据类型数组然后for循环的方式;

package com.test.day01;
// public class TestParam {
//创建一个求和的方法 //定义一个求和的方法
public int sum(int [] arr){
int sum =0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static void main(String[] args) {
TestParam testParam = new TestParam();
int [] a={6542,3,5,7,4,2,4,67,8,3,56,8,534,3,2,6,6,8,9,4,2};
int num = testParam.sum(a);
System.out.println(num);
} }

定义一个数组,存储3名学员的基本信息,编写方法计算学员的总成绩。

package com.test.day01;
class Student{
//姓名 性别 年龄 分数
double scoer;
char sex;
String name;
int age;
}
//管理类
class Manager{
public double sum(Student [] stus){
int sum =0;
for (int i = 0; i < stus.length; i++) {
sum += stus[i].scoer;
}
return sum;
}
}
public class TestStudent { public static void main(String[] args) {
// TODO Auto-generated method stub
Student xiaolongnv = new Student();
xiaolongnv.scoer=100;
Student mucheng = new Student();
mucheng.scoer=84.5;
Student zzhangruocheng = new Student();
zzhangruocheng.scoer=69.3;
Student [] stus={xiaolongnv,mucheng,zzhangruocheng};
double num = new Manager().sum(stus);
System.out.println(num);
} }

可变参数

可变参数的底层是数组  他的优点是传参灵活,注意多个参数的时候可变参数只能放在最后。

public class TestVarParam {

    public void f(String s ,int [] arr,int [] arr1) {
System.out.println(Arrays.toString(arr));
}
public void ff(int ...arr ) {// int []arr
System.out.println(Arrays.toString(arr));
System.out.println(arr.length);
}
public static void main(String[] args) {
TestVarParam test = new TestVarParam();
// 1
// int [] arr = {11,22,33};
// int [] arr = new int[]{11,22,33};
// test.f(arr);
// 2
// test.f(new int [] {11,22,33});
// 3.
test.ff();// new int []{}; 长度为0 的数组
test.ff(111);// new int[]{111}
test.ff(11,22,33,44);// new int[]{11,22,33,44};
test.ff(new int[] {11,22}); } }

递归

递归就是方法自身调用自身,但是一定要有出口

package com.test.day01;

public class Recursion {

    int count=0;
public void f(){
count++;
if(count==10){
return;
}
System.out.println("慢慢学,这要学的快");
f();
} public static void main(String[] args) {
// 使用递归的方法输出10个“慢慢学,这要学的快”
Recursion re = new Recursion();
re.f();
} }

使用递归实现阶乘

package com.test.day01;

public class Factorial {
public int fa(int n){
if(n==1){
return 1;
}else{
return n*fa(n-1);
}
}
public static void main(String[] args) {
// 使用递归实现阶乘 阶乘形式:1*2*3*4*5*6*7
Factorial factorial = new Factorial();
System.out.println(factorial.fa(6));
} }

最新文章

  1. Front End Developer Questions 前端开发人员问题(三)JavaScript部分
  2. 生成CIL的问题
  3. 在ArcGIS中如何进行POI点抽稀
  4. Why NSAttributedString import html must be on main thread?
  5. Commons CLI - Option Properties
  6. mybatis怎样配置数据库集群
  7. [Mugeda HTML5技术教程之5] 创建新作品
  8. [转]XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks
  9. python命令行解析工具argparse模块【2】
  10. C++中 auto自己主动变量,命名空间,using作用以及作用域
  11. ASP.NET页面之间传递值的几种方式(转载)
  12. ArrayList和List主要区别 就是ArrayList类型不安全。
  13. Docker学习总结(一)
  14. OpenJudge_1321:棋盘问题
  15. Java数组的创建和初始化
  16. Java复制目录/子目录/文件
  17. git-ftp代码部署方式
  18. A Method for the Construction of Minimum-Redundancy Codes
  19. Jmeter If Controller中设置多个条件用“与”进行连接
  20. 使用TestFlight邀请外部人员測试APP

热门文章

  1. org.springframework.expression.spel.SpelEvaluationException: EL1030E
  2. [WEB安全]Dirsearch工具命令
  3. firewall-cmd命令详解
  4. golang基于当前1.9版本进行源码编译升级到1.10
  5. 使用docker运行mysql
  6. keras 在train_on_batch中启用tensorboard
  7. MySQL VARCHAR字段最大长度到底是多少
  8. jmeter -- beanshell 执行本地py文件
  9. 源码安装LNMP
  10. Mysql备份工具mysqldump和mysqlhotcopy