博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中传值和传址
阅读量:7027 次
发布时间:2019-06-28

本文共 3156 字,大约阅读时间需要 10 分钟。

hot3.png

本文用一下代码说明:

Java代码  

简单字符串String:

  1. package luojing;  
  2. public class StringDemo  
  3. {  
  4.     public static void main(String[]args)  
  5.     {  
  6.         String str=new String("hello");  
  7.         //调用函数改变str的值  
  8.         change(str);  
  9.         System.out.println(str);  
  10.           
  11.     }  
  12.       
  13.     public static void change(String str1)  
  14.     {  
  15.         str1+="luojing";  
  16.     }  
  17. }  
程序执行结果: 
hello

StringBuffer:

  1. public class StringDemo  
  2. {  
  3.     public static void main(String[]args)  
  4.     {  
  5.         StringBuffer str=new StringBuffer("hello");  
  6.         //调用函数改变str的值  
  7.         change(str);  
  8.         System.out.println(str);  
  9.           
  10.     }  
  11.       
  12.     public static void change(StringBuffer str1)  
  13.     {  
  14.         str1.append("luojing");  
  15.     }  
  16. }  
程序运行结果
:hello luojing
       这个时候str的内容就改变了。

将上面的StringBuffer换成我们自己定义的对象:

  1. public class test  
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.         Demo demo=new Demo("hello");  
  6.         //调用函数该变demo.name的值  
  7.         change(demo);  
  8.         System.out.println(demo.getName());  
  9.       
  10.     }  
  11.       
  12.     public static void change(Demo d)  
  13.     {  
  14.         d.setName("luojing");  
  15.     }  
  16.   
  17. }  
  18.   
  19. class Demo  
  20. {  
  21.     private String name;  
  22.       
  23.     public Demo(String s)  
  24.     {  
  25.         name=s;  
  26.     }  
  27.     public String getName()  
  28.     {  
  29.         return name;  
  30.     }  
  31.     public void setName(String str)  
  32.     {  
  33.         name=str;  
  34.     }  
  35. }  
程序运行结果: luojing  和我们使用StringBuffer对象是效果相同。

我们再对change()方法做一些修改:

  1. package luojing;  
  2. public class test   
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         Demo demo=new Demo("hello");  
  7.         //调用函数该变demo.name的值  
  8.         change(demo);  
  9.         System.out.println(demo.getName());  
  10.       
  11.     }  
  12.       
  13.     public static void change(Demo d)  
  14.     {  
  15.         Demo d1=new Demo("hello java");  
  16.         d=d1;     
  17.     }  
  18.   
  19. }  
  20.   
  21. class Demo  
  22. {  
  23.     private String name;  
  24.       
  25.     public Demo(String s)  
  26.     {  
  27.         name=s;  
  28.     }  
  29.     public String getName()  
  30.     {  
  31.         return name;  
  32.     }  
  33.     public void setName(String str)  
  34.     {  
  35.         name=str;  
  36.     }  
  37. }  
运行结果:
hello
     可以看到,虽然我们在change()方法中对d进行了改变,而实际的对象demo并没有改变。

  1. class Foo {  
  2.    private int x;  
  3.    public Foo(int x) {  
  4.       this.x = x;  
  5.     }  
  6.     public void setX(int x) {  
  7.        this.x = x;  
  8.     }  
  9.     public int getX() {  
  10.        return x;  
  11.     }  
  12. }  
  13.   
  14. public class Submit {  
  15.     static Foo fooBar(Foo foo) {  
  16.         foo = new Foo(100);  
  17.         return foo;  
  18.    }  
  19.    
  20.     public static void main(String[] args) {  
  21.         Foo foo = new Foo(300);  
  22.         System.out.print(foo.getX() + "-");  
  23.    
  24.         Foo fooFoo = fooBar(foo);  
  25.         System.out.print(foo.getX() + "-");  
  26.         System.out.print(fooFoo.getX() + "-");  
  27.    
  28.         foo = fooBar(fooFoo);  
  29.         System.out.print(foo.getX() + "-");  
  30.         System.out.print(fooFoo.getX());  
  31.     }  
  32. }  

What is the output of the program shown in the exhibit? 
A. 300-100-100-100-100 
B. 300-300-100-100-100 
C. 300-300-300-100-100 
D. 300-300-300-300-100 
Answer: B 
涉及知识点: 
1.Java中的参数传递有传值和传址两种; 
2.基本类型和String型作为参数时,为传值方式,只把值传入方法,不管在方法中怎么处理这个参数,原值不变; 
3.其他引用类型作为参数时,为传址方式,将指向内存中的地址传入方法,方法中此内存地址中的值发生变化时,原值也会改变; 
4.例外: 
    (1)如果引用类型的对象通过传址方式将其指向内存中的地址传入方法后,方法中使用new关键字重新给参数赋值时,会在内存中重新开辟空间,参数指向新的内存空间,此时参数和原对象指向的就不是同一个地址了,参数值的变化不会改变原值; 
    (2)String型是引用类型,但是String型作为参数,是传值方式,可以通过以下两种方式来理解: 
        <1>String本质上是基本类型的char[],基本类型作为参数时,为传值方式; 
        <2> 字符串在内存中是存储在堆中的一个常量,String对象指向内存中这个常量的地址,通过传址方式将地址传入方法后,方法中如果通过字符串给参数赋值,则会重新在堆中创建一个字符串常量,并指向这个地址,原值依然指向原来的字符串常量地址,参数值的变化不会改变原值,如果通过new关键字给参数赋值,参见 (1)中的解释。 
解析: 
1.“Foo foo = new Foo(300);”,此时foo.getX()的值为300; 
2.“Foo fooFoo = fooBar(foo);”,因为Foo是引用类型,main方法中的foo通过传址的方式将其指向的地址传给fooBar方法中的foo,此时两个foo指向同一个地址,foo.getX()的值都为300;通过“new Foo(100)”给fooBar方法中的foo赋值后,该foo重新指向了一个新的地址,foo.getX()的值为新地址中的值100,而main方法中的foo仍然指向原来的地址,foo.getX()的值没有改变,仍为 300;fooBar将foo的值返回给fooFoo,因此fooFoo.getX()的值为100; 
3.“foo = fooBar(fooFoo);”,同2中的解释,foo.getX()的值变为100,fooFoo.getX()的值没有变化,仍为100; 

转载于:https://my.oschina.net/u/1012289/blog/133044

你可能感兴趣的文章
mysql优化
查看>>
自动化常识
查看>>
js实现倒计时
查看>>
C#保存文件为无BOM的utf8格式
查看>>
MVC、MVP以及MVVM分析
查看>>
解决Android Studio 错误方法
查看>>
kindeditor用法简单介绍
查看>>
bson.errors.InvalidStringData: strings in documents must be valid UTF-8
查看>>
浅析Java中的访问权限控制
查看>>
Topological Sorting
查看>>
这个是我们公司的面试题。 特此共享
查看>>
【每日算法】排序算法总结(复杂度&amp;稳定性)
查看>>
JavaWeb学习总结(十三)——使用Session防止表单重复提交(转)
查看>>
tushare
查看>>
【ZT】Oracle CASE WHEN 用法介绍
查看>>
LeetCode--028--实现strStr() (java)
查看>>
LeetCode--027--移除元素
查看>>
matlab练习程序(渲染三原色)
查看>>
slice splice substr substring
查看>>
NSIS检测操作系统x64还是x86的问题。
查看>>