zoukankan      html  css  js  c++  java
  • java传值和传址

    public class CallValueOrAddress{  
        private String name;  
        private int age;  
      
            public void setName(String name){  
                this.name=name;  
                }  
            public String getName(){  
                return name;  
                }  
      
            public void setAge(int age){  
                this.age=age;  
                }  
            public int getAge(){  
                return age;  
                }  
      
            public static void main(String agrs[]){  
                String str_name="wangxianxing";  
                String new_name="nihao";  
                int int_age=21;  
                int new_age=22;  
                System.out.println("传值引用");  
                System.out.println("姓名:"+str_name+"年龄:"+int_age);  
                new_name=str_name;  
                new_name=new String("nihaoma?");  
                new_age=int_age;  
                new_age=22;  
                System.out.println("姓名:"+str_name+"年龄:"+int_age);  
      
                CallValueOrAddress cvoa=new CallValueOrAddress();  
                cvoa.setName("wangxianxing");  
                cvoa.setAge(21);  
                System.out.println("传址引用");  
                System.out.println("姓名:"+cvoa.getName()+"年龄:"+cvoa.getAge());  
                CallValueOrAddress new_cvoa=cvoa;  
                new_cvoa.setName("kingxianstar");  
                new_cvoa.setAge(22);  
                System.out.println("姓名:"+cvoa.getName()+"年龄:"+cvoa.getAge());  
                System.out.println("例外情况一");  
                new_cvoa=new CallValueOrAddress();  
                new_cvoa.setName("kingxianstar+kingxianstar");  
                new_cvoa.setAge(32);  
                System.out.println("姓名:"+cvoa.getName()+"年龄:"+cvoa.getAge());  
                }  
      
      
        }  

    运行结果如下: 
    传值引用 
    姓名:wangxianxing年龄:21 
    姓名:wangxianxing年龄:21 
    传址引用 
    姓名:wangxianxing年龄:21 
    姓名:kingxianstar年龄:22 
    例外情况一 
    姓名:kingxianstar年龄:22 

    感觉还不是很全面,所以在网上又搜索了一下,发现了这篇(http://mgc.name/article.asp?id=842) 

    class Foo {  
       private int x;  
       public Foo(int x) {  
          this.x = x;  
        }  
        public void setX(int x) {  
           this.x = x;  
        }  
        public int getX() {  
           return x;  
        }  
    }  
      
    public class Submit {  
        static Foo fooBar(Foo foo) {  
            foo = new Foo(100);  
            return foo;  
       }  
       
        public static void main(String[] args) {  
            Foo foo = new Foo(300);  
            System.out.print(foo.getX() + "-");  
       
            Foo fooFoo = fooBar(foo);  
            System.out.print(foo.getX() + "-");  
            System.out.print(fooFoo.getX() + "-");  
       
            foo = fooBar(fooFoo);  
            System.out.print(foo.getX() + "-");  
            System.out.print(fooFoo.getX());  
        }  
    }  


    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; 

  • 相关阅读:
    快速入门系列--GIT版本控制工具
    快速入门系列--NOSQL--05Redis也木有那么“高富帅”
    apt下载open-jdk8报错add-apt-repository: command not found
    jenkins-参数化构建(三)插件:Git Parameter
    jenkins-参数化构建(二)插件:Extended Choice Parameter
    jenkins-参数化构建(一)
    Jenkins-权限管理
    redis安装
    elk-图形化展示(八)
    elk-(七)
  • 原文地址:https://www.cnblogs.com/happyPawpaw/p/3399974.html
Copyright © 2011-2022 走看看