java 中函数的参数传递详细介绍

网友投稿 198 2023-06-29


java 中函数的参数传递详细介绍

java中函数的参数传递

总结:

1.将对象(对象的引用)作为参数传递时传递的是引用(相当于指针)。也就是说函数内对参数所做的修改会影响原来的对象。

  2.当将基本类型或基本类型的包装集作为参数传递时,传递的是值。也就是说函数内对参数所做的修改不会影响原来的变量。

  3.数组(数组引用))作为参数传递时传递的是引用(相当于指针)。也就是说函数内对参数所做的修改会影响原来的数组。

  4.String类型(引用)作为参数传递时传递的是引用,只是对String做出任何修改时有一个新的String对象会产生,原来的String对象的值不会做任何修改。(但是可以将新的对象的       引用赋给原来的引用,这样给人的表面现象就是原来的对象变了,其实没有变,只是原来指向它的引用指向了新的对象)。

package StringTest;

class A{

int a=1;

char b='A';

public A(){}

public A(int _a,char _b){

this.a=_a;

this.b=_b;

}

public String toString(){

return "a="+this.a+",b="+this.b;

}

}

public class ReferenceTest {

public static A changeA(A classa){

classa.a=2;

classa.b='B';

return classa;

}

public static String changeString(String str){

System.out.println(str.hashCode());

str=str.toLowerCase();

System.out.println(str.hashCode());

return str;

}

public static int changeint(int a){

a=a+1;

return a;

}

public static Integer changeInteger(Integer a){

a=new Integer(9);

return a;

}

public static int[] changeintarray(int a[]){

a[0]=10;

return a;

}

public static void printArray(int a[]){

for(int i=0;i

System.out.print(a[i]+" ");

}

System.out.println();

}

public static void main(String[] args) {

//自定义的对象传递的是引用

A a=new A();

A b=changeA(a);

System.out.println(a);

System.out.println(b);

System.out.println("----------------------");

//String对象作为参数传递的也是引用(只是String对象的值不能变,每一个修改String对象的值都会重新创建一个新的String对象用以保存修改后的值,原来的值不会变)

String str1="HUHUALIANG";

System.out.println(str1.hashCode());

String str2=changeString(str1);

System.out.printlnhttp://(str2.hashCode());

System.out.println(str1);

System.out.println(str2);

System.out.println("----------------------");

//基本类型是值传递

int inta=8;

int intb=changeint(inta);

System.out.println(inta);

System.out.println(intb);

System.out.println("----------------------");

//基本类型的包装集作为参数传递的是值而不是引用

Integer c=new Integer(1);

Integer d=changeInteger(c);

System.out.println(c);

System.out.println(d);

System.out.println("----------------------");

//数组传递的是引用

int [] arraya={0,1,2,3};

int [] arrayb=changeintarray(arraya);

printArray(arraya);

printArray(arrayb);

}

}

运行结果:

a=2,b=B

a=2,b=B

----------------------

711139030

711139030

226046678

226046678

HUHUALIANG

huhualiang

----------------------

8

9

----------------------

1

9

----------------------

10 1 2 3

10 1 2 3

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

System.out.print(a[i]+" ");

}

System.out.println();

}

public static void main(String[] args) {

//自定义的对象传递的是引用

A a=new A();

A b=changeA(a);

System.out.println(a);

System.out.println(b);

System.out.println("----------------------");

//String对象作为参数传递的也是引用(只是String对象的值不能变,每一个修改String对象的值都会重新创建一个新的String对象用以保存修改后的值,原来的值不会变)

String str1="HUHUALIANG";

System.out.println(str1.hashCode());

String str2=changeString(str1);

System.out.printlnhttp://(str2.hashCode());

System.out.println(str1);

System.out.println(str2);

System.out.println("----------------------");

//基本类型是值传递

int inta=8;

int intb=changeint(inta);

System.out.println(inta);

System.out.println(intb);

System.out.println("----------------------");

//基本类型的包装集作为参数传递的是值而不是引用

Integer c=new Integer(1);

Integer d=changeInteger(c);

System.out.println(c);

System.out.println(d);

System.out.println("----------------------");

//数组传递的是引用

int [] arraya={0,1,2,3};

int [] arrayb=changeintarray(arraya);

printArray(arraya);

printArray(arrayb);

}

}

运行结果:

a=2,b=B

a=2,b=B

----------------------

711139030

711139030

226046678

226046678

HUHUALIANG

huhualiang

----------------------

8

9

----------------------

1

9

----------------------

10 1 2 3

10 1 2 3

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java实现敏感词过滤实例
下一篇:详解java开发webservice的几种方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~