• 关于java中函数参数传递的两种方式的总结


    类似于C++中函数参数的传递方式,java由于语言体系中不存在指针的概念,所以C++有3种参数传递方式;而java种只有两种。下面是我的一点体会:

    分别为:引用类型传递和基本数据类型传递。引用传递本质上并没有新创建对象,而是声明了另一个引用来指向同一个对象。而基本数据类型的参数传递是值传递,在内存中是拷贝出另一份。以下是代码解释:

     1 package asgh;
     2 
     3 public class RefDemo {
     4 
     5     public static void main(String[] args) {
     6         Student zs;
     7         zs=new Student();
     8         zs.name="zhangsan";
     9         a(zs);
    10         System.out.println(zs.name);
    11         
    12         int[] arr=new int[] {1,2,3,4,5,6};
    13         b(arr);
    14         System.out.println(arr[0]);
    15         
    16         int i=100;
    17         int j=200;
    18         c(i,j);
    19         System.out.println(i+"  "+j);
    20 
    21     }
    22 
    23     public static void c(int  i, int j) {
    24         int temp=0;
    25         temp=i;
    26         i=j;
    27         j=temp;
    28         
    29     }
    30 
    31     public static void b(int[] arr) {
    32         arr[0]=300;
    33         
    34     }
    35 
    36     public static void a(Student zs) {
    37         zs.name="lisi";
    38         
    39     }
    40 
    41 }
    42 
    43 class Student{
    44     String name;
    45     int age;
    46     String address;
    47 }
    code everywhere everytime!
  • 相关阅读:
    Swift中的可选链与内存管理(干货系列)
    Swift中的类型转换
    Swift中类与结构的初始化
    Swift3中函数的使用
    Java常用的公共方法
    Eclipse中添加文档注释快捷键
    SVN服务器的搭建(三)
    SVN服务器的搭建(二)
    SVN服务器的搭建(一)
    多线程常见的例子
  • 原文地址:https://www.cnblogs.com/vcyy/p/7846460.html
Copyright © 2020-2023  润新知