java-参数传递(小白篇)

参数传值分为:值传递以及引用传递(地址传递)。(个人理解-勿喷)

参数分为基本类型,对象等。

1.基本类型(8大类型:int/float/double/char/long/boolean/byte/short)

例子:

/**
 * @description:
 * @author: lc
 * @time: 2018/2/23 0023
 */
public class Test01 {
    public static void main(String[] args) {
        int count=1;
        System.out.println("改变前...count="+count);
        change(count);
        System.out.println("改变后...count="+count);
    }
    public static void change(int count){
        count=count+1;
    }
}

结果:


改变前...count=1
改变后...count=1

Process finished with exit code 0

说明:对于基本类型,只是把值(副本)传递进去。由于String是不可变对象,所以在这里和基本类型一样的效果

2.对象

例子

Student类:

/**
 * @description:
 * @author: lc
 * @time: 2018/2/23 0023
 */
public class Student {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

测试类

/**
 * @description:
 * @author: lc
 * @time: 2018/2/23 0023
 */
public class Test {
    public static void main(String[] args) {
        Student s1=new Student();
        s1.setName("李四");
        change1(s1);
        System.out.println("s1.getName():"+s1.getName());

    }
    public static void  change1(Student s2){
        Student s3=new Student();
        s3.setName("张三1");
        s2.setName("张三");
        s2=s3;
    }
}

结果:


s1.getName():张三

Process finished with exit code 0

结果为什么是张三?看下面分析

分析图




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值