Java学习笔记01-传值or传引用

突然发现Java在传值和引用部分上,借助C语言里面的指针(底层内存分配)可以很容易理解,或者者说这些难点都是C语言的遗留问题和Java无关

Note:关于传值与传引用的定义可参考下方链接

Java 方法的参数是按什么传递的问题,其答案就只能是:即是按值传递也是按引用传递,只是参照物不同,结果也就不同 Java传值与引用

直接上代码

/*
*This Program demonstrate parameter passing in java
*@Version 1.00 
*@Author Scorpion
*/

public class ParamTest{
    public static void main(String[] args){
        /**
        *Test1: Methods can't modified numeric parameters
        */
        System.out.println("TestTrip Value...");
        double percent = 10;
        System.out.println("Before: percent="+percent);
        tripleValue(percent);
        System.out.println("After: percent="+percent);

        /**
        *Test2: Method can change the state of object parameters
        */
        System.out.println("\nTesting tripleValue:");
        Employee harry = new Employee("Harry",5000);
        System.out.println("Before: salary = "+harry.getSalary());
        tripleSalary(harry);
        System.out.println("After: salary = "+harry.getSalary());

        /**
        *Test3: Methods can't attach new objects to object parameter
        */
        System.out.println("Testing swap:");
        Employee a = new Employee("Alice",70000);
        Employee b = new Employee("Bob",60000);
        System.out.println("Before: a=" + a.getName());
        System.out.println("Before: b=" + b.getName());
        swap(a,b);
        System.out.println("After: a=" + a.getName());
        System.out.println("After: b=" + b.getName());
    }

    public static void tripleValue(double x){
        x = 3 * x;
        System.out.println("End of Method:x="+x);
    }

    public static void tripleSalary(Employee x){
        x.raiseSalary(200);
        System.out.println("End of Method x = "+x.getSalary());
    }
    /**
    *Note: just like pointer
    */
    public static void swap(Employee x,Employee y){
        Employee temp = x;
        x =  y;
        y = temp;
        System.out.println("End of Method: x="+x.getName());
        System.out.println("End of Method: y="+y.getName());
    }
}

class Employee{//simplified Employee class
    private String name;
    private double salary;

    public Employee(String n,double s)
    {
        name = n;
        salary = s;
    }

    public String getName()
    {
        return name;
    }

    public double getSalary()
    {
        return salary;
    }

    public void raiseSalary(double byPercent)
    {
        double raise = salary * byPercent / 100;
        salary += raise;
    }
}

OUTPUT

TestTrip Value...
Before: percent=10.0
End of Method:x=30.0
After: percent=10.0
Testing tripleValue:
Before: salary = 5000.0
End of Method x = 15000.0
After: salary = 15000.0
Testing swap:
Before: a=Alice
Before: b=Bob
End of Method: x=Bob
End of Method: y=Alice
After: a=Alice
After: b=Bob

对应C语言片段片段

Part1: tripleValue

void tripleValue(int a,int b)
{
  int tmp;
  tmp = a;
  a = b;
  b = tmp;
}

void main()
{
  int a = 10;
  int b = 9;
  tripleValue(a,b);
}

Part2:tripleSalary

void tripleSalary(int * salary)
{
   *salary +=2000;
}

void main()
{
  int salary = 5000;
  int* p = &salary;
  tripleSalary(p);
}

Part3:Swap

void swap(int* a,int* b)
{
  int* tmp = null;
  tmp = a;
  a = b;
  b = tmp;
}

void main()
{
  int a = 9;
  int b = 10;
  int *pa = &a;
  int *pb = &b;

  swap(pa,pb);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,785评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,058评论 19 139
  • 一. Java基础部分.................................................
    wy_sure阅读 3,845评论 0 11
  • 对象与引用对象 对象引用对于一个简单的例子:Person p=new Person("Jim",18);内存表示:...
    fredal阅读 1,548评论 0 8
  • 在2017年,这样一个信息化发展了好长时间的年代里,网络是大多数人日常生活的一部分,无论是工作还是休闲,网络不仅仅...
    小虎王阅读 234评论 0 2