Differences between Java value types and reference types

Source: Internet
Author: User

There is no doubt that the difference between the value of the function and the reference is that the transfer value is a copy of the data, and the reference is to manipulate the object.

However, it is easy to overlook the fact that a reference is actually a copy of a reference.

Examples are as follows:

Class number

{

public int num;

}

public class Test

{

public static void Main (string[] str)

{

Number A=new number ();

A.num=1;

Number B=new number ();

b.num=2;

Swap (A, b);

System.out.println (a.num+ "" +b.num);

}

public static void swap (number A,number B)

{

Number temp;

Temp=b;

B=a;

A=temp;

}

}

Output Result: 1 2

Principle: In the Swap function, A and b simply pass a copy of the reference and do not affect the actual A and B.

Differences between Java value types and reference types

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.