Different instances of value types and reference types in Java (iii)

Source: Internet
Author: User

There is no doubt that the difference between a function pass and a function reference is that it is possible to manipulate an object by passing a copy of the data to the reference.

But what most people ignore is that a reference is actually a copy of a reference.

Examples are as follows:

1 class Number2 {3      Public intnum;4 }5  Public classTopcoder6 {7      Public Static voidMain (string[] args)8     {9Number a=NewNumber ();TenA.num=1; OneNumber b=NewNumber (); Ab.num=2; - Swap (A, b); -System.out.println (a.num+ "" +b.num); the     } -      -     Static voidSwap (number A,number b) -     { + Number temp; -temp=b; +b=A; AA=temp; at     } -}

Output is:

1 2

The analysis is as follows:

Before swap

Reference a----------->number (num:1)

Reference b----------->number (num:2)

In swap

Because the function simply passes a copy of the reference and does not have any effect on references A and B, it can be ignored.

After swap

Since the SWAP function has no effect on references A and B, and the swap function does not change the object content, there is no change

Different instances of value types and reference types in Java (iii)

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.