Java Foundation _0310: Reference delivery

Source: Internet
Author: User

Reference delivery

Reference passing is the most confusing concept in Java, but in the actual development, the reference transfer has a very important role;
The core of reference passing is that the same heap memory space is pointed to by different stack memory;

Example: first Reference delivery example
class Message {    private int num = 10;       // 定义int基本类型的属性    /**     * 本类没有提供无参构造方法,而是提供有参构造,可以接收num属性的内容     * @param num 接收num属性的内容     */    public Message(int num) {        this.num = num; // 为num属性赋值    }    public void setNum(int num) {        this.num = num;    }    public int getNum() {        return this.num;    }}public class TestDemo {    public static void main(String args[]) {        Message msg = new Message(30);  // 实例化Message类对象同时传递num属性内容        fun(msg);       // 引用传递        System.out.println(msg.getNum());   // 输出num属性内容    }    /**     * 修改Message类中的num属性内容     * @param temp Message类的引用     */    public static void fun(Message temp) {        temp.setNum(100);       // 修改num属性内容    }}程序执行结果: 100

Example: Second citation example
public class TestDemo {    public static void main(String args[]) {        String msg = "Hello";       // 定义String类对象        fun(msg);           // 引用传递        System.out.println(msg);    // 输出msg对象内容    }    public static void fun(String temp) {   // 接收字符串引用        temp = "World";     // 改变字符串引用    }}程序执行结果:Hello

Example: Third reference pass
class Message {    private String info = "此内容无用" ;     // 定义String类型属性         public Message(String info) {       // 利用构造方法设置info属性内容        this.info = info ;    }    public void setInfo(String info) {        this.info = info ;    }    public String getInfo() {        return this.info ;    }}public class TestDemo {    public static void main(String args[]) {        Message msg = new Message("Hello") ;    // 实例化Message类对象        fun(msg) ;                              // 引用传递        System.out.println(msg.getInfo()) ; // 输出info属性内容    }    public static void fun(Message temp) {  // 接收Message类引用        temp.setInfo("World") ;     // 修改info属性内容    }}程序执行结果:World

Java Foundation _0310: Reference delivery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.